Why does my object's position change when I rotate it?
I have been working on a movement script for a player model, I want it to rotate freely along the y axis and not move its position. However, every time I hit play the object rotates but it also moves its position uncontrollably and glitch through other objects. please help?? Here is my code;
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public float speed = 10f; public float jumpSpeed = 8f; public float gravity = 30f; private Vector3 moveDir = Vector3.zero;
void FixedUpdate()
{
transform.Rotate (0f,10f * Time.deltaTime,0f);
}
void Update()
{
CharacterController controller = gameObject.GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
moveDir = transform.TransformDirection(moveDir);
moveDir *= speed;
if (Input.GetButtonDown("Jump"))
{
moveDir.y = jumpSpeed;
}
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
Help is greatly appreciated!!
Your answer
Follow this Question
Related Questions
Press on object, and be able to control that object you pressed on. 0 Answers
How to make a 2d platformer with a rolling ball as a player? 1 Answer
How i can set Start Position and Ending position in ROTATION AROUND AXIS? 0 Answers
ridgidbody AddForce in direction player is pointed? 1 Answer
Face direction of a Vector 3 1 Answer