- Home /
controller.SimpleMove slower when Character go Backward
Hello guys ! For my character, I would like to make him go slower when he go backward. I know that the script using controller.SimpleMove is quite a simple script but I don't thinks I need too much for my game (it's a Resident Evil-like so my player doesn't have to have many direction) Here the script (I think a lot of you already know what it is) #pragma strict
var speed : float = 3.0;
var speedBack : float = 2.0;
var rotateSpeed : float = 3.0;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var backward : Vector3 = transform.TransformDirection(Vector3.back);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
var curSpeedBack : float = speedBack * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetKey("left shift"))
{
speed = 6.0;
}
else
{
speed = 3.0;
}
}
@script RequireComponent(CharacterController)
I remade it a little bit but a don't know how to put the var "speedBack" on controller.SimpleMove because I think he only use forward.
Thanks for helping me :)
Answer by shadows_s · Jun 23, 2014 at 04:45 PM
var speed : float = 3.0;
var speedBack : float = 2.0;
var rotateSpeed : float = 3.0;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var backward : Vector3 = transform.TransformDirection(Vector3.back);
// --------------------------------------------------
var verticalAxis : float = Input.GetAxis ("Vertical");
var curSpeed : float;
if( verticalAxis > 0 ) // going forward
{
curSpeed = speed * verticalAxis;
}
else // going backward
{
curSpeed = speedBack * verticalAxis;
}
// here speed accordingly to direction
controller.SimpleMove(forward * curSpeed );
if(Input.GetKey("left shift"))
{
speed = 6.0;
}
else
{
speed = 3.0;
}
}
@script RequireComponent(CharacterController)
Thank you man ! It was easy I feel really bad about this haha
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Follow script problem 1 Answer
CharacterController unexpectedly moving up 0 Answers
Character rolls consistently in all directions, but right. 1 Answer
Scripting help 1 Answer