- Home /
Basic movement not working?
So, This Basic Movement script, causes me to have a break-dancing cube which is uncontrollable.
#pragma strict
var speed = 1.0;
var rotateSpeed = 3.0;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
}
@script RequireComponent(CharacterController)
Please note, this is BEFORE i hit a button aswell as after
Answer by LPGaming · Jan 16, 2013 at 10:03 AM
I figured it out, it's because I had a Box Collider already, and was using a script to create a CharacterController, My guess was that they were colliding with eachother, causing the Box Collider to "Bounce"
Answer by Lachee1 · Jan 16, 2013 at 09:32 AM
Instead of using Input.GetKeyDown(KeyCode.W)
go trhough unity's inputs. so use this code instead,
if( Input.GetAxis ("Vertical") > 0)//You only want the positive
transform.position += transform.forward * moveSpeed * Time.deltaTime;
For more information, go to the GetAxis documentation, found here: http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxis.html
Not sure I understand which part of this will make it move when I hit W? didn't see that in the code the reference provided.
Also, this code just makes it move continuously without stop, so wouldn;t i still be using the GetkeyDown code?
Answer by senad · Jan 16, 2013 at 09:34 AM
Your problem is not in this part of code, because it looks fine.
You should have a look at what changes you made between recently (start of broken behaviour) and now to find out what code broke your movement. :)
See that's the thing, I didn't do anything.... I just opened my project.... and Bam... Although I had a script that was doing the movement, I deleted it, because I had it turning by using the Transform.rotate(0, 90, 0); which was horrible, so I started changing it, that was also in C# and I moved to JavaScript to do movement because it seem easier, not having to make temeporary value for everything