- Home /
Cant Get Character To Move
Hi there, I'm quite new to 3d games and the models that come with them. The issues i'm having as the title says,i'm unable to get the character to move.
Originally I used a humanoid model and after watching some tutorials on how to get the character to move i found that this code, in javascript, did the job...
function Update ()
{
if(Input.GetButton(LeftButton))
{
transform.rotation.y = LeftRotate;
transform.Translate(Forward * MoveSpeed * DT);
}
if(Input.GetButton(RightButton))
{
transform.rotation.y = RightRotate;
transform.Translate(Forward * MoveSpeed * DT);
}
}
These are the values im using for interest sake.
Forward = new Vector3(1,0,0);
DT = Time.deltaTime;
I want the game to be 3d but from a 2d perspective so what code does is rotate the model to either left or right and move forward, and it worked. This morning I changed my model to a better suited model for my game, the model is Attack Bot from the asset store. The main difference between the attack bot and the humanoid model is that the attack bot is made of different pieces, for example the left wheel,right wheel,body, ect are separate parts whereas the humanoid was 1 object.
I've applied the movement code to the robot and it rotates him to the direction i want to go but he doesn't move. Im completely stumped as to what the issue could be.
I've attached a screenshot to give you a view of what i'm rambling about.
Sorry for the long post, tried to get as much info in as I could. Thanks /D
Answer by maccabbe · Apr 19, 2015 at 10:28 PM
Since you state that you use DT=Time.deltaTime but it doesn't show up in your Update code, I'm going to assume you use it somewhere else such as Start or Awake.
Time.deltaTime is the time since the last frame so it changes from frame to frame. If you declare a variable DT that stores the value of Time.deltaTime and do not update it then the variable will copy the time since the last frame and keep it that way. If you attempt to use DT as though it has the current Time.deltaTime without updating DT then you are going to have the wrong value of DT. If you declare DT=Time.deltaTime in the first frame then since there is no previous frame the time since the previous frame is 0 which would result in all movements being multiplied by 0.
Change your code so it either updates DT in the Update function
function Update ()
{
DT = Time.deltaTime;
if(Input.GetButton(LeftButton))
{
transform.rotation.y = LeftRotate;
transform.Translate(Forward * MoveSpeed * DT);
}
if(Input.GetButton(RightButton))
{
transform.rotation.y = RightRotate;
transform.Translate(Forward * MoveSpeed * DT);
}
}
or so it uses Time.deltaTime instead of DT
function Update ()
{
if(Input.GetButton(LeftButton))
{
transform.rotation.y = LeftRotate;
transform.Translate(Forward * MoveSpeed * Time.deltaTime);
}
if(Input.GetButton(RightButton))
{
transform.rotation.y = RightRotate;
transform.Translate(Forward * MoveSpeed * Time.deltaTime);
}
}
Hi there, Thanks for the reply and I understand what you're saying about the Time.deltatime being in the update function and i'll keep it in $$anonymous$$d for next time. But alas the change didn't help, the robot still flips but doesn't move while the humanoid works correctly.
Check the values of $$anonymous$$oveSpeed, Forward, and DT
if(Input.GetButton(LeftButton))
{
transform.rotation.y = LeftRotate;
transform.Translate(Forward * $$anonymous$$oveSpeed * Time.deltaTime);
Debug.Log(Forward+" "+$$anonymous$$oveSpeed+" "+Time.deltaTime+" "+(Forward*$$anonymous$$oveSpeed*Time.deltaTime));
}
I saw what the error was thanks to the Debug.Log, I forgot to give movespeed a value so it was moving forward at 0. I feel quite silly >.< Thank you very much for your help !
Your answer
Follow this Question
Related Questions
Translating Javascript code into C# Code 2 Answers
I'm trying to get an object to move using javascript however it won't move, can someone help? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
JavaScript not working 0 Answers