The question is answered, right answer was accepted
I can't move my player in a fps?
Hi, I followed an online tutorial (by Brackeys) on making a fps. I got to a point where I have made the player, and I am coding the movement. I have followed everything correctly, but when Brackeys' checks the game, he can move. Whereas, I can not. I have both of my scripts ready if asked for, here is player controller - using UnityEngine;
[RequireComponent(typeof(PlayerMotor))] public class PlayerController : MonoBehaviour {
[SerializeField]
private float speed = 5f;
private PlayerMotor motor;
void Start ()
{
motor = GetComponent<PlayerMotor>();
}
void Update ()
{
// Calculate movement velocity as a 3D Vector
float xMov = Input.GetAxisRaw("Horizontal");
float zMov = Input.GetAxisRaw("Vertical");
Vector3 movHorizontal = transform.right * xMov;
Vector3 movVertical = transform.forward * zMov;
// Final movement vector
Vector3 velocity = (movHorizontal + movVertical).normalized * speed;
// Apply movement
motor.Move(velocity);
}
} And here is Player Motor - using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class PlayerMotor : MonoBehaviour {
private Vector3 velocity = Vector3.zero;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Gets a movement vector
public void Move (Vector3 velocity)
{
velocity = velocity;
}
// Run every physics iteration
void FixedUpdate ()
{
PerformMovement();
}
//Perform movement based on velocity variable
void PerformMovement ()
{
if(velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
} } }
Answer by DFT-Games · May 29, 2017 at 11:05 PM
Hi @Inverrtted ,
You have an ambiguous naming in your Player Motor due to the velocity parameter that is also a field. You can solve it in two ways, one is to write the Move method like this:
public void Move(Vector3 velocity)
{
this.velocity = velocity;
}
The other is to just thence the parameter name, like this:
public void Move(Vector3 newVelocity)
{
velocity = newVelocity;
}
Both solutions work ;)
Thank you! I was annoyed with myself, glad it's done now. You're a lifesaver! :)
Follow this Question
Related Questions
Some problems with handmade basic player controller 0 Answers
How do I get a character to walk on walls and ceilings? 1 Answer
Why can my character controlled player can stand on the side of a block? 0 Answers
Top Down Character - Face direction of movement? 0 Answers
Object Player Movement stops working after Restarting Unity 0 Answers