I am making a first person shooter and I am using rigid body Move Rotation to rotate my character due to physics in that scene. It’s moving the object but not the transform of the object do when I use Rigid body MovePosition for my wasd controls, it works from its original point instead of where the object is looking. Is there any way to fix this. I have tried doing a transform command separately but it didn’t work. I am using a player controller script for my values and my player motor script to do the grunt work. I am using all of the default unity libraries. Here is the player controller script:
[RequireComponent(typeof(PlayerMoter))]
public class PlayerControler : MonoBehaviour {
[SerializeField]
private float speed = 5f;
[SerializeField]
private float rotSpeed = 5f;
[SerializeField]
private PlayerMoter moter;
void Start ()
{
moter = GetComponent<PlayerMoter>();
}
void Update ()
{
float Xmovment = Input.GetAxisRaw ("Horizontal");
float Zmovment = Input.GetAxisRaw ("Vertical");
float mouseX = Input.GetAxisRaw ("Mouse X");
Vector3 finalVelocity = new Vector3(Xmovment, 0f, Zmovment).normalized * speed;
Vector3 finalRotVelocit = new Vector3 (0f, mouseX, 0f).normalized * rotSpeed;
Quaternion finalRotQuat = Quaternion.Euler (finalRotVelocit*Time.fixedDeltaTime);
moter.setVelocity (finalVelocity);
moter.setRotation (finalRotQuat);
}
}
here is the player motor script:
public class PlayerMoter : MonoBehaviour {
Rigidbody rb;
Vector3 velocity = Vector3.zero;
Quaternion rotaion;
void Start () {
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
move ();
rotate ();
}
public void setVelocity(Vector3 direction)
{
velocity = direction;
}
public void setRotation(Quaternion rot)
{
rotaion = rot;
}
public void move()
{
if(velocity!=Vector3.zero)
{
rb.MovePosition (rb.position + velocity * Time.fixedDeltaTime);
}
}
public void rotate()
{
rb.MoveRotation (rb.rotation * rotaion);
}
}
Thanks for any help I can get.