x


Movement Script Help

How would I change this script so that instead of making the player move forward it makes it go backwards? Ive tried changing a few variables (I think there called) but I keep getting an error saying "unity.transform doesnt have a definition of backward" or something like that. Please can anyone help me?

using UnityEngine;

public class walkforward : MonoBehaviour{

public float WalkSpeed = 5.0f;

CharacterController controller;



void Start(){

    controller = (CharacterController) transform.GetComponent("CharacterController");

}



void Update(){



    Vector3 movVel = Vector3.zero;



    if (Input.GetKey("w")){

       animation.CrossFade("walk_forward");

       movVel = transform.forward * WalkSpeed;

    }

    else {

       //if we're not moving we'll want to crossfade back to the idle animation

       animation.CrossFade("idle");

       float verticalAxisInput = Input.GetAxis("Vertical");

       if (verticalAxisInput != 0)

         movVel = transform.forward * verticalAxisInput * WalkSpeed;

    }

    // move the character with specified movVel velocity

    controller.SimpleMove(movVel);

}

}

more ▼

asked Oct 24 '11 at 03:29 PM

Dreave gravatar image

Dreave
122 82 94 96

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

This is really one of the simplest things you will ever have to do(if I understand you correctly) If you want to do this without any scripting required, why don't you just change the variable from 5.0 to -5.0(a negative value)?:

using UnityEngine;
public class walkforward : MonoBehaviour{

public float WalkSpeed = -5.0f;

CharacterController controller;



void Start(){

    controller = (CharacterController) transform.GetComponent("CharacterController");

}



void Update(){



    Vector3 movVel = Vector3.zero;



    if (Input.GetKey("w")){

       animation.CrossFade("walk_forward");

       movVel = transform.forward * WalkSpeed;

    }

    else {

       //if we're not moving we'll want to crossfade back to the idle animation

       animation.CrossFade("idle");

       float verticalAxisInput = Input.GetAxis("Vertical");

       if (verticalAxisInput != 0)

         movVel = transform.forward * verticalAxisInput * WalkSpeed;

    }

    // move the character with specified movVel velocity

    controller.SimpleMove(movVel);

    }
}

There are many ways of doing it, and you don't know the basics(which is not a bad thing), but starting out with a few tutorials will work wonders and will properly help you with this problem.

more ▼

answered Oct 24 '11 at 03:59 PM

OrangeLightning gravatar image

OrangeLightning
5.3k 47 57 111

I didn't know this because I thought it would be a lot harder to do so but thanks for the help

Oct 24 '11 at 04:54 PM Dreave
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5058
x4141
x3721

asked: Oct 24 '11 at 03:29 PM

Seen: 639 times

Last Updated: Oct 24 '11 at 04:54 PM