Can't rotate and move at the same time - c#

I'm currently developing an FPS shooter in Unity 3D. I'm fairly new to Unity and I've been having a bit of trouble with my player movement script. Individually everything seems to work, I can rotate and move the player freely, however when I try doing the two simultaneously my player seems to lock and won't rotate.
Sometimes jumping or moving to higher ground on the terrain seems to fix the issue, however I ran a few checks with gravity disabled, no colliders, and with the player well above ground, so the problem seems to be with the script. I've also done a bit of debugging and the Rotate() code does run, just the rotation amount doesn't seem to change.
Here is the player movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMov : MonoBehaviour
{
[SerializeField]
private Camera cam;
[SerializeField]
private float speed = 5f;
[SerializeField]
private float looksensitivity = 4f;
[Header("Camera View Lock:")]
[SerializeField] //min and max amount for looking up and down (degrees)
private float lowlock = 70f;
[SerializeField]
private float highlock = 85f;
private Rigidbody rb;
private float currentrotx; //used later for calculating relative rotation
public Vector3 velocity;
public Vector3 rotation; // rotates the player from side to side
public float camrotation; // rotates the camera up and down
public float jmpspe = 2000f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
CalcMovement();
CalcRotation();
Rotate();
}
void FixedUpdate()
{
Move();
}
private void CalcRotation()
{
float yrot = Input.GetAxisRaw("Mouse X"); //around x axis
rotation = new Vector3(0f, yrot, 0f) * looksensitivity;
float xrot = Input.GetAxisRaw("Mouse Y"); //around y axis
camrotation = xrot * looksensitivity;
}
private void CalcMovement()
{
float xmov = Input.GetAxisRaw("Horizontal");
float zmov = Input.GetAxisRaw("Vertical");
Vector3 movhor = transform.right * xmov;
Vector3 movver = transform.forward * zmov;
velocity = (movhor + movver).normalized * speed;
}
void Move()
{
//move
if (velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
//jump
if (Input.GetKeyDown(KeyCode.Space))
{
//add double jump limit later!
rb.AddForce(0, jmpspe * Time.deltaTime, 0, ForceMode.Impulse);
}
}
void Rotate()
{
//looking side to side
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
// camera looking up and down
currentrotx -= camrotation;
currentrotx = Mathf.Clamp(currentrotx, -lowlock, highlock);
cam.transform.localEulerAngles = new Vector3(currentrotx, 0, 0);
}
}
Here are the relevant components attached to my player:
(The player has a couple more components attached but I ran tests without them and the problem still occurs)
Like I said I'm a bit of a unity novice, so i'm sure I missed something small but I just can't seem to place my finger on it, I've been stuck on this for a while so any help is much appreciated.

SOLVED:
It seems the problem I had was because I was running the scene from my laptop and not a desktop which I assume is what the Unity input was built for.

Related

How to make a rigidbody moving at a constant speed using the velocity function go up and down slopes?

In my game, I have a vehicle using a rigidbody that moves forward (relative to the front of the vehicle) at a constant rate using the rigidbody velocity function. The vehicle also may turn 90 degrees to the right and back, of course causing the direction it is moving to rotate. The vehicle drives on a floating platform, and if driven off, the velocity function is stopped so that the car may properly fall off using rigidbody physics.
I want to have slopes at various points in the game, and since the vehicle moves at a constant speed, the vehicle should be able to drive up and down a slope automatically. The issue is that I have to directly control the rigidbody's velocity to make it drive at a constant speed, so when the vehicle drives into a slope, the velocity function keeps pushing it forward without properly rotating, not letting it properly drive up the slope. How should I go about fixing this? Should I be using the rigidbody velocity function at all in this case?
For reference, here is the script I am using for vehicle movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField] private GameObject followCamera;
[SerializeField] private AnimationCurve lerpCurve;
[SerializeField] private float turnSpeed;
[SerializeField] private float speed;
private static float turnTimer = 2f;
private float turnTimeElapsed;
private float clampedTimeElapsed;
private float lerpTimer;
Rigidbody ridgy;
LayerMask groundLayer;
private void Start()
{
groundLayer = LayerMask.GetMask("Ground");
ridgy = this.GetComponent<Rigidbody>();
turnTimeElapsed = 0f;
Quaternion endRot = Quaternion.Euler(0f, 90f, 0f);
}
private void Update()
{
if (Input.GetKey(KeyCode.Space) && turnTimeElapsed < turnTimer)
{
turnTimeElapsed += turnSpeed * Time.deltaTime;
}
else if (!Input.GetKey(KeyCode.Space) && turnTimeElapsed > 0)
{
turnTimeElapsed -= turnSpeed * Time.deltaTime;
}
clampedTimeElapsed = Mathf.Clamp(turnTimeElapsed, 0f, turnTimer);
lerpTimer = clampedTimeElapsed / turnTimer;
lerpTimer = lerpCurve.Evaluate(lerpTimer);
}
private void FixedUpdate()
{
if (Physics.CheckBox(transform.position, new Vector3(.5f, .5f, .5f), Quaternion.identity, groundLayer))
{
if (lerpTimer > 0f && lerpTimer < .75f)
{
speed = 45;
}
else
{
speed = 15;
}
ridgy.rotation = new Quaternion
(
0,
Mathf.Sin(lerpTimer * Mathf.PI / 4f),
0,
Mathf.Cos(lerpTimer * Mathf.PI / 4f)
);
ridgy.velocity = transform.forward * speed;
}
else
{
followCamera.GetComponent<CameraFollow>().enabled = false;
return;
}
}
}

The CineMachine Camera ruin the 2d parallax Effect because its shaking

I am working in a 2d platform game I applied cinemachine camera and parallax script to give a good effect ,but the parallax is shaking and vibrating hard , I found out that the cinamchine was the reason because the camera it shaking, when I disabled the cinemachine it work smoothly
here is the parallax code
private float startpos;
private GameObject cam;
[SerializeField] private float parallax;
[SerializeField] private float speed = 0.1f;
// Start is called before the first frame update
void Start()
{
cam = GameObject.Find("Main Camera");
startpos = transform.position.x;
}
// Update is called once per frame
void Update()
{
float distance = (cam.transform.position.x * parallax);
transform.position = new Vector3(startpos + distance, transform.position.y, transform.position.z);
}
and the settings of the MC vcam1
enter image description here
please any help I dont find any on with that problem
You could try setting the CinemachineBrain update method to Manual Update and then just calling ManualUpdate() in another script.
using UnityEngine;
using Cinemachine;
public class ManualBrainUpdate : MonoBehaviour
{
public CinemachineBrain cineBrain;
public float smoothTime;
private void Start()
{
cineBrain = GetComponent<CinemachineBrain>();
}
void Update()
{
cineBrain.ManualUpdate();
}
}
To avoid any kind of jitter in Unity (like what the camera is adding to distance when it shakes) use the various SmoothDamp functions e.g. https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html:
public float smoothTime = 0.3f; // adjust this to taste
private float distance;
private Vector3 velocity = Vector3.zero;
private Vector3 targetPos
void Update()
{
distance = cam.transform.position.x * parallax;
targetPos = new Vector3(startpos + distance, transform.position.y, transform.position.z)
transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime);
}

How can I repeat this boost (push)?

I dont understand how I can make this shotgun jumps in my version of Unity: https://www.youtube.com/watch?v=FUkdz8jYt3w
How does this work?
Im using "First Person Controller" that looks like capsule
There are images: https://imgur.com/a/uvfAePX
This is for old version of Unity 4.5.5. I've tried RigidBody, but nothing happened. I've tried Transform, but again, no result.
First Person Controller:
using UnityEngine;
using System.Collections;
public class run : MonoBehaviour
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public Transform character;
public int CharacterForce = 5000;
public int time = 1;
void Update()
{
CharacterController controller =
GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
if (Input.GetMouseButtonDown(0))
{
Transform BulletInstance = (Transform)Instantiate(character, GameObject.Find("CameraRSP").transform.position, Quaternion.identity);
BulletInstance.GetComponent<Rigidbody>().AddForce(transform.forward * CharacterForce);
}
}
}
"Grenade" code:
using UnityEngine;
using System.Collections;
public class grenade : MonoBehaviour
{
public Transform GrenadeF;
public int force = 500;
public float radius;
void Start()
{
Collider[] col = Physics.OverlapSphere(transform.position, radius);
foreach (Collider c in col)
{
c.GetComponent<Rigidbody>().AddExplosionForce(force, transform.position, radius);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, radius);
}
}
When I used Transform I expected that this would work in ANY direction, but it worked in only one.
When I used RigidBody I expected that this would WORK, but my capsule didnt even moved.
Checkout the optional upwardsModifier parameter of
AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius, float upwardsModifier = 0.0f, ForceMode mode = ForceMode.Force));
(The API for Unity 4 is no longer available but I guess it should have been the same there)
Adjustment to the apparent position of the explosion to make it seem to lift objects.
and
Using this parameter, you can make the explosion appear to throw objects up into the air, which can give a more dramatic effect rather than a simple outward force. Force can be applied only to an active rigidbody.
By default it is 0 so if you don't pass it there won't be any upwards force.
As you can see in the example from the API
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach (Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
// |
// v
if (rb) rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
}
they passed e.g. 3.0f as upwardsModifier. This makes the explosion
appear to be centred 3.0 units below its actual position for purposes of calculating the force direction (ie, the centre and the radius of effect are not modified).
Note: Typed on smartphone so no warrenty but I hope the idea gets clear

transform.Translate isn't cutting it and rigidbody.AddRelativeForce doesn't move my character (Unity and c#)

I'm making a game in Unity 5 (3D) and am using c# for scripting. So I basically have a third person dude running around with 8 direction controls (WASD) and the way I have set it up now is like this (ignore all the public variables for testing):
public class Player8D : MonoBehaviour
{
public float moveSpeed;
public float moveSpeedMax = 13.5F;
public float turnSpeed = 4F;
public float transitionSpeed = 10F;
private float moveSmooth = 0.0F;
public float moveSmoothTime = 10F;
private Animator anim;
private Rigidbody rb;
private CharacterController cc;
void Start()
{
anim = gameObject.GetComponentInChildren<Animator>();
rb = gameObject.GetComponentInChildren<Rigidbody>();
cc = gameObject.GetComponentInChildren<CharacterController>();
}
void Update()
{
Vector3 NextDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if (NextDir != Vector3.zero) //When movement input is happening
{
anim.SetInteger("Run", 1); //Animation stuff
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(NextDir), Time.deltaTime * turnSpeed);
moveSpeed = Mathf.SmoothDamp(moveSpeed, moveSpeedMax, ref moveSmooth, moveSmoothTime / 100);
} else
{
moveSpeed = Mathf.SmoothDamp(moveSpeed, 0.00F, ref moveSmooth, transitionSpeed / 100);
anim.SetInteger("Run", 0); //Animation stuff
anim.CrossFade("Idle", transitionSpeed / 100);
}
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed); //Movement code
}
}
This gave me the best feeling due to how it moves in the direction it's facing, rather than in 8 distinct world directions like with cc.Move. That being said it has the caveat of not being in physics, which I kinda need for OnTriggerEnter and falling realistically and it makes going uphill strange. I have no idea how to manage the rb.AddForce or AddRelativeForce functions, as they don't move my character when I replace transform.Translate with them, no matter how great the force is.
How do I change the code and components to get my character moving through physics?
Here is a screenshot of my scene hierarchy:

How to make a Game Object point towards the mouse in Unity? (C#)

I am creating a game involving a turret and it needs to "point" (that is, rotate) to the mouse. It's in 3-D environment, but at a bird's eye view. So for my purposes we are in a 2-D environment.
Here is my code:
using UnityEngine;
using System.Collections;
public class Turret : MonoBehaviour {
// Use this for initialization
void Start () {
}
int speed; float friction; float lerpSpeed ; private float xDeg ;
private float yDeg; private Quaternion fromRotation; private Quaternion toRotation;
void Update () {
xDeg -= Input.GetAxis ("Mouse X"); yDeg += Input.GetAxis ("Mouse Y");
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
}
}
If you could tell me what I'm doing wrong or give me the correct code that would be great! Please note that I am using a C# script.
Input and rotation calculations are not right.
xDeg -= Input.GetAxis ("Mouse X"); yDeg += Input.GetAxis ("Mouse Y");
toRotation = Quaternion.Euler(yDeg,xDeg,0);
You are making a top down game. So I assume that you are trying to aim at where mouse points on a 2D plane, which is ground. You should get your input not based on mouse axes but taking account where your mouse cursor is.
That being said, you can use this method to achieve your goal:
public class CharacterInput : MonoBehaviour
{
public Transform CharacterTransform;
void Update()
{
var groundPlane = new Plane(Vector3.up, -CharacterTransform.position.y);
var mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDistance;
if (groundPlane.Raycast(mouseRay, out hitDistance))
{
var lookAtPosition = mouseRay.GetPoint(hitDistance);
CharacterTransform.LookAt(lookAtPosition, Vector3.up);
}
}
}
And to rotate it smoothly:
public class CharacterInput : MonoBehaviour
{
public Transform CharacterTransform;
public float RotationSmoothingCoef = 0.1f;
void Update()
{
var groundPlane = new Plane(Vector3.up, -CharacterTransform.position.y);
var mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDistance;
if (groundPlane.Raycast(mouseRay, out hitDistance))
{
var lookAtPosition = mouseRay.GetPoint(hitDistance);
var targetRotation = Quaternion.LookRotation(lookAtPosition - CharacterTransform.position, Vector3.up);
var rotation = Quaternion.Lerp(CharacterTransform.rotation, targetRotation, RotationSmoothingCoef);
CharacterTransform.rotation = rotation;
}
}
}
Better calculate smoothing in FixedUpdate to make it independent of frames per second. So it rotates at the same speed on every computer configuration:
public class CharacterInput : MonoBehaviour
{
public Transform CharacterTransform;
public float RotationSmoothingCoef = 0.01f;
private Quaternion targetRotation;
void Update()
{
var groundPlane = new Plane(Vector3.up, -CharacterTransform.position.y);
var mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDistance;
if (groundPlane.Raycast(mouseRay, out hitDistance))
{
var lookAtPosition = mouseRay.GetPoint(hitDistance);
targetRotation = Quaternion.LookRotation(lookAtPosition - CharacterTransform.position, Vector3.up);
}
}
void FixedUpdate()
{
var rotation = Quaternion.Lerp(CharacterTransform.rotation, targetRotation, RotationSmoothingCoef);
CharacterTransform.rotation = rotation;
}
}
I think this is a common mistake for a Unity beginner (as I had it wrong the first time as well).
As you probably know by now, the Update() method is called every new frame.
So, every new frame (in your code) you calculate where the mouse is, how to rotate and call Lerp.
What you probably miss is how Lerp works and that is interpolating the motion by making one step every frame , i.e every time Lerp is called it rotates (in your case) by some interval. Your interval is Time.deltaTime * lerpSpeed which changes every frame since Time.deltaTime is the time between 2 consecutive frames.
So to make Lerp work properly (== smooth interpolation) you must call it with the same start and end position and complete the interpolation between them (call Lerp from 0 to 1 with as many intervals as you wish).
What I suggest you do is move this code:
xDeg -= Input.GetAxis ("Mouse X"); yDeg += Input.GetAxis ("Mouse Y");
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
to a different place (one which Update has access to these variables) and:
Set toRotation everytime it should move (xDeg can be computed along side)
Remove fromRotation and instead call Lerp like this:
Lerp(transform.rotation, toRotation,Time.deltaTime * lerpSpeed)

Categories