- Home /
Variable Script "movement"
var Move forward : Keyboard;
// replace 'move forward' with what??
// replace 'keyboard' with what??
// i want to know how to make var move forward with value to set its speed
Answer by duck · Apr 15, 2010 at 06:33 AM
Variable names can't contain spaces. Perhaps you're looking for some kind of very simple movement script, along these lines?
var moveSpeed = 5; var rotateSpeed = 50;
function Update() {
var h = Input.GetAxis("Vertical"); var v = Input.GetAxis("Horizontal");
var moveAmount = h moveSpeed Time.deltaTime; var turnAmount = v rotateSpeed Time.deltaTime;
transform.translate(0,0,moveAmount); transform.rotate(0,turnAmount,0);
}
(allows you to move and rotate the object, with the arrow keys)
Answer by DarkHype · Apr 15, 2010 at 07:19 AM
You could also just duplicate the FPS Walker script that comes with the standard assets and delete horizontal line! here this is my edited FPS walker script! :
var speed = 6.0;
var jumpSpeed = 8.0; var gravity = 20.0;
private var moveDirection = Vector3.zero; private var grounded : boolean = false;
function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
if (Input.GetButton ("Jump")) { moveDirection.y = jumpSpeed; } }
// Apply gravity moveDirection.y -= gravity * Time.deltaTime;
// Move the controller var controller : CharacterController = GetComponent(CharacterController); var flags = controller.Move(moveDirection * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
Hope i helped you!
Hmmm... looks like to code function of unity answers is a little wierd
All you have to do is select your code and hit the code button, it's easy.
@Eric5h5, he's right - I've had circumstances where the code button just refused to format the first half-dozen lines of my code, no idea why. So yeah, it's easy - when it works... :)
Your answer
Follow this Question
Related Questions
Changing variable from another script 1 Answer
movement 2d in a grid 3 Answers
How to modify variables from scripts in standard package? 2 Answers
Player movement issue... 1 Answer
Car movement script ? 1 Answer