- Home /
How to make my character always run in the same direction?
I'm trying to make a game such as Subway Surfer or Temple run. But with my script, the run animation works but my character is not moving. Here is my scipt:
var MainCharacter : CharacterController;
var MoveDirection:Vector3;
var speed : float = 100;
var runSpeed : float = 6.0;
public var runAnimation : AnimationClip;
public var runMaxAnimationSpeed : float = 1.0;
private var _animation : Animation;
private var moveDirection : Vector3 = Vector3.zero;
function Update()
{
if(!runAnimation)
{
_animation = null;
Debug.Log( AnimationClip);
}
}
Could you please help me
thanks
Answer by SterlingStudios · Sep 28, 2014 at 05:24 PM
This should work.
var speed : float = 6;
var direction : Vector3 = Vector3.fwd;
var runAnimation : AnimationClip;
function Update () {
transform.Translate(direction * speed * Time.deltaTime);
animation.Play(runAnimation.name);
}
Warning: Not Tested Code
Now, how could I make my character turn? Thanks for the help
Or you can just do.
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)){ transform.Rotate(0,-5,0); }
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)){ transform.Rotate(0,5,0); }
Not sure what temple run is or anything, but that's another alternative to rotating.
And if you want to use Rigidbody Physics to move forward and stuff just add a Rigidbody to your Player. (Change chage the Vector3.forward) to the directions you want and you will move that direction.
and add this in a FIXEDUPDATE
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)){ transform.rigidbody.AddRelativeForce (Vector3.forward * 10);
EDIT - I noticed again that your thread said ALWAYS run in same direction.. In update just put transform.Translate(Vector3.Forward); or whatever direction you want him to go.
the problem with both scripts is that my character diseppears and and then reappears in the right place. We can't see him turn. I don't know if you've understand but how could I solve this? thanks
Answer by SterlingStudios · Oct 03, 2014 at 10:14 PM
Considering you mean like a temple run move, you could try this (add it in on line 11 or 12):
if(Input.GetKeyDown(KeyCode.LeftArrow)) {
transform.position.x -= turnAmmount;
//cant remember if its x or z, so if x doesnt work try z
}
if(Input.GetKeyDown(KeyCode.RightArrow)) {
transform.position.x += turnAmmount;
//cant remember if its x or z, so if x doesnt work try z
}
And add some new variables:
var turnAmmount : float = 2;
Again, untested code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Does Anyone Have a Halo-Like Scripts? 1 Answer
Already has a definiton for 1 Answer
How to create a Game Tree data structure in Unity. 0 Answers
Javascript error.. insert semicolon?? 2 Answers