Hi, I am trying to make an FPS controller and am currently trying to add movement. I am using addRelativeForce but it pretty much just slides all over the place. I tried doing this:
if(Physics.Raycast(transform.position, Vector3.down, 1))
{
if((Input.GetAxisRaw("Horizontal") != 0) || (Input.GetAxisRaw("Vertical") != 0))
{
rigidbody.drag = 0;
}else
{
rigidbody.drag = 10;
}
}else
{
rigidbody.drag = 0;
}
However, this just makes it so you stop correctly after letting go of the keys. While you are moving, you still slide, so I need another way of doing this. The full code I use is this:
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour
{
public CameraScript cameraScript;
private float cameraRotationY;
private float movementForceVertical;
private float movementForceHorizontal;
public float movementSpeedHorizontal = 20f * 50;
public float movementSpeedVertical = 30f * 50;
void Start ()
{
}
void Update ()
{
cameraRotationY = cameraScript.getRotation();
transform.rotation = Quaternion.Euler(0,cameraRotationY,0);
movementForceHorizontal = Input.GetAxisRaw("Horizontal") * movementSpeedHorizontal;
movementForceVertical = Input.GetAxisRaw("Vertical") * movementSpeedVertical;
if(Physics.Raycast(transform.position, Vector3.down, 1))
{
if((Input.GetAxisRaw("Horizontal") != 0) || (Input.GetAxisRaw("Vertical") != 0))
{
rigidbody.drag = 0;
}else
{
rigidbody.drag = 10;
}
}else
{
rigidbody.drag = 0;
}
}
void FixedUpdate()
{
rigidbody.AddRelativeForce(movementForceHorizontal, 0, movementForceVertical);
}
}
There is probably a way more efficient way of doing some of the things, but I am still learning C#. Thanks!