So I’ve been using Unity for a while now and I came across a problem I couldn’t fix from day one.
Basically, my character can’t move around properly(I am sure it has nothing to do with code), I am using a Capsule as my gameObject and I have a Capsule Collider attached to it, the thing is it floats on the air, some people told me to add a Rigidbody, I did that and atleast he can walk on completely plain gameobjects or Terrains, but whenever he climbs up a mountain for example he starts spinning on both X/Y/Z axises like objects in real life would do in a no gravity zone.
I am not sure what is causing this problem, but I do know that my Capsule Collider is not a trigger, neither is my Terrain Collider.
In case some of you need to see my code I can’t acces it right now but it is pretty basic i use transform.Translate function to move around, and for my Jump function I added a empty GameObject as a child of my Capsule(player), and attached a capsule Collider on the gameobject, its right below my Player so whenever it touches the ground my Jump boolean is enabled(OnTriggerEnter).
And there’s your problem, don’t ever use transform.Translate, if you want to move an object with physics. Use rigidbody.AddForce instead, or you can set the rigidbody.velocity, but that is not recommended, because it’s makes non-realistic movement
If you don’t want it to rotate around, when climbing mountain, you can just freeze the rotations in the rigidbody component
It doesn’t matter, that neither of those is a trigger, that just means, that it activates the onTriggerEnter() event
public class BetterMovement : MonoBehaviour {
public Rigidbody rb;
public float PlayerSpeed = 80;
public float RotateSpeed = 120;
// Use this for initialization
void Start () {
rb = GetComponent ();
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W)){
rb.AddForce(transform.forward * PlayerSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S)){
rb.AddForce(transform.forward * PlayerSpeed );
}
if(Input.GetKey(KeyCode.A)){
transform.Rotate(-Vector3.up * RotateSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D)){
transform.Rotate(Vector3.up * PlayerSpeed * Time.deltaTime);
}
}
}
I tried this and it only works for rotating now
Okay now it’s almost all good but it feels like im walking on ice all the time, and the character is not moving forward/backwars at my current rotation, its moving on places it shouldnt
instead of: rb.AddForce(transform.forward * PlayerSpeed * Time.deltaTime);, just do:
rb.AddForce(transform.forward * PlayerSpeed * Time.deltaTime, ForceMode.VelocityChange);
I think this way your force needs to be smaller, then you could cap the velocity by:
if (rb.velocity.Magnitude > PlayerSpeed) {
rb.velocity = rb.velocity.Normalized * PlayerSpeed;
}
And you messed up your code a little in line 25, you wrote PlayerSpeed instead of RotateSpeed
If you want to move backwards, put a - before the transform.forward
Thank you so much everything worked and now hes just going too fast, but i gues this is for fixing it
if(rb.velocity > PlayerSpeed){
rb.velocity = rb.velocity.Normalize * PlayerSpeed;
}
-the problem is that codeblock is giving me an error: Operator >' cannot be applied to operands of type UnityEngine.Vector3’ and `float’
yes i did it like that this is the code:
if(rb.velocity.Magnitude > PlayerSpeed){
rb.velocity = rb.velocity.normalized * PlayerSpeed;
}
it finds an error at if(rb.velocity.Magnitude > PlayerSpeed)