- Home /
Issue with velocity movement script
Hi!
Recently I changed my script for movement from transform.position to the following movement:
if (Input.GetKey (KeyCode.W))
{
if (IsMoving [0] == false)
{
GetComponent <Rigidbody> ().velocity += transform.forward * Speed;
IsMoving [0] = true;
}
}
else if (IsMoving [0] == true)
{
GetComponent <Rigidbody> ().velocity -= transform.forward * Speed;
IsMoving [0] = false;
}
problem is when i walk into a wall and let go of w, it'll move me back. i thought subtracting forward velocity would stop me from moving, but when colliding with something in front of me and letting go of w, i move backwards, How can i improve this script? thanks!
EDIT
So I've been thinking, and i thought maybe what i need is a way to remove all forward velocity… how to do?
Answer by Addyarb · Mar 27, 2015 at 10:48 PM
Your script confuses me, but I understand what it's doing.
If I weren't to rewrite the script, I would just add:
void OnTriggerEnter(Collider col){
if(col.tag == "Wall"){
GetComponent<Rigidbody>().velocity = 0;
}
}
thats not the problem. the problem is when i collide with the wall, which automatically lowers my velocity. then i let go of w, and i move backwards.
Okay, hold on. I'll test it out. Trying to understand what you mean.
How about:
if(Get$$anonymous$$eyUp($$anonymous$$eyCode.W))
velocity *=-1;
i don't think so but ill try
EDIT
no it didn't work sorry to say…
basically what im trying to do is make a move script with rigidbody.velocity.
Your answer
Follow this Question
Related Questions
Movement using rigidbody.velocity to apply a constant force until stop 1 Answer
Space Shooter Tutorial Player Won't move. 1 Answer
Velocity for movement 0 Answers
Moving rigidbody upwards 1 Answer
Rigidbody sticks to wall 0 Answers