- Home /
Players can't move through objects, but they can glitch through it.
Hello, players can't walk through objects however they can glitch through it i suppose is the word when you continue to walk forward. I am unsure if this is a scripting issue or perhaps something with collides but I've been having this issue for a while and some help would be much appreciated. Also I am using Photon servers if that has anything to do with it. Thanks.
Here is my character controller script that I think may have something to do with it. I'm not too sure.
using UnityEngine; using System.Collections;
public class characterController : MonoBehaviour {
public float speed = 10.0F;
// Use this for initialization
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
Answer by Mukabr · Oct 04, 2016 at 09:53 PM
Try using Rigidbody.velocity to move your player.
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.velocity = new Vector3(0, 10, 0);
}
Remember to use inside FixedUpdate() function.
Hey, thank you for your response, I really appreciate it. I should have said i'm that I'm a bit of a noob at Unity and I am a bit confused on what to modify here. A lot of the code I use was from a tutorial and I'm unsure what to change specifically, again sorry for my lack of knowledge on this stuff however further guidance would be really good help. Thanks in advance.
You should do something like
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
rb.velocity = transform.TransformDirection(new Vector3(straffe, 0, translation));
Note: "velocity" is a velocity. It's applied automatically each physics frame, so you must not multiply by deltaTime.
Also note that this code could stay in Update as it's not a big difference when it's in FixedUpdate. FixedUpdate has to be used when you apply continuous forces. Since the Input can only change between Update calls, reading it in FixedUpdate has no advantages.