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

2 Answers

2

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

Thanks for your answers. both scripts works.

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

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.