- Home /
Player phases through walls and terrain
It was working a few months ago, I had my player run up and down a slope hundreds of times and now it won't work. What do I need to fix? Here's my code: using UnityEngine;
public class Movecube : MonoBehaviour { public float speed = .1f;
// Update is called once per frame void Update() { float xDirection = Input.GetAxis("Horizontal"); float zDirection = Input.GetAxis("Vertical");
Vector3 moveDirection = transform.right * xDirection + transform.forward * zDirection;
transform.position += moveDirection * speed;
}
}
Hi, I also can't figure out why, but there's problem i see in your movement code. I think you need to multiply the "speed" to "Time.deltaTime" to make it more suitable to different devices or fame rates. @demonsigma
Oh, I didn't even notice that! Thank you!
That's right. But I think it still does not solve the actual problem. When modifying transform the player is more or less teleported and might be in some collider, falling through it. You probably have a Rigidbody on the player. To simulate the collision correctly you should use the commands for the Rigidbody. Rigidbody.MovePosition should work (https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html). Often however Rigidbody.AddForce() / Add RelativeForce() are used to get a more natural movement with acceleration.
agreed! It seems like i didn't understand the question clearly.
Answer by Zendoth · Jun 13, 2021 at 05:44 PM
Does your player and wall/terrain have some kind of collider?
Your answer
Follow this Question
Related Questions
Convert Audio Beeps And Dashes into Text 0 Answers
GetItemByID error 0 Answers