- Home /
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);
}
}
Answer by FLASHDENMARK · Oct 24, 2011 at 03:59 PM
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.
I didn't know this because I thought it would be a lot harder to do so but thanks for the help