- Home /
Question by
patrikgalloherny · Jan 25, 2020 at 06:15 PM ·
c#movementmovement scriptnewbie
My character moves when i dont want to
I have a problem. If I move the cursor up, the character will begin to move forward. The same thing happens if I move the cursor down. Thank you!
Here are my codes
public class PlayerMovement : MonoBehaviour
{
private CharacterController characterController;
[SerializeField] float jumpSpeed = 20f;
[SerializeField] float gravity = 1f;
[SerializeField] float moveSpeed = 5f;
float yVelocity = 0f;
public float moveHori;
public float moveVert;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update() // Update is called once per frame
{
moveHori = Input.GetAxis("Horizontal");
moveVert = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(moveHori, 0, moveVert);
Vector3 velocity = direction * moveSpeed;
if (characterController.isGrounded)
{
if (Input.GetButtonDown("Jump"))
{
yVelocity = jumpSpeed;
}
}
else
{
yVelocity -= gravity;
}
velocity.y = yVelocity;
velocity = transform.TransformDirection(velocity);
characterController.Move(velocity * Time.deltaTime);
}
}
public class MouseMovement: MonoBehaviour
{
[SerializeField] float sensitivityX = 5f;
float mouseX = 0.0f;
[SerializeField] float sensitivityY = 5f;
public float minimumY = -30f;
public float maximumY = 30f;
float rotationY = 0f;
void Update()
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
Comment
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Making the camera follow the mouse but stay near the player? 2 Answers
[3D] Top-Down Make Character move in the direction of the position he is currently looking at? 1 Answer
Question about movement with Rigidbody | Diagnol movement going faster 2 Answers
Smooth Movement with set distance 1 Answer