- Home /
Once I apply my movement script, the collision on my character gets turned off. What is wrong with this script?
Once i apply my movement script to my character cube, the collision on the character is turned off and the character can go through objects that have box collision.
public class PlayerMovement : MonoBehaviour {
public float forwardSpeed = 20f;
public float backwardsSpeed = 15f;
public float leftStrafeSpeed = 10f;
public float rightStrafeSpeed = 10f;
CharacterController player;
// Use this for initialization
void Start()
{
player = this.GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.position += transform.forward * Time.deltaTime * forwardSpeed;
}
else if (Input.GetKey(KeyCode.S))
{
transform.position -= transform.forward * Time.deltaTime * backwardsSpeed;
}
else if (Input.GetKey(KeyCode.A))
{
transform.position -= transform.right * Time.deltaTime * leftStrafeSpeed;
}
else if (Input.GetKey(KeyCode.D))
{
transform.position += transform.right * Time.deltaTime * rightStrafeSpeed;
}
}
}
this is the code. Am i doing something wrong. The overall movement works but there no is collision going.
Answer by SK-ColdFire · Oct 31, 2018 at 06:43 PM
So what do you mean by Once "I apply my movement script, the collision on my character gets turned off" ? You mean when you add the script to the character in the editor?
I'm not any expert sorry but i will try to help you as much as i can!
Answer by Vega4Life · Oct 31, 2018 at 07:49 PM
I don't see anything that would mess with collision within this script, but if you are using Unity's CharacterController script, make sure your character object has a rigid body on it. Adding a CharacterController script doesn't do this automatically.
Answer by RustyCrow · Oct 31, 2018 at 09:21 PM
First: I would check like the others are saying. Do i have my Object set up for allowing collisions sorted.
Second: Moving something using Transform.postion might give some unwanted results when it comes to Collision but i am not 100% sure on this, i will need someone with more experienced to confirm. But try this/ Official Tutorial. Hope this helps get you closer to an answer @Defane1