- Home /
Moving character towards direction fast
Hi everyone, I'm new here and to unity. I'm not an expert at programming but I'm trying my best to get around. I have recently started working on a game, which will contribute to my final year degree at University. The problem I have is that I wan't to implement a button which makes the character move from one place to another quick, like rolling on the ground to get to another place depending on which location the camera is facing.
This is the script I have written so far, I appreciate your help guys, seriously.
using UnityEngine;
using System.Collections;
public class BasicMovement : MonoBehaviour {
public float moveSpeed = 8;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float moveForward = moveSpeed * Time.smoothDeltaTime * Input.GetAxis("Vertical");
float moveLeft = moveSpeed * Time.smoothDeltaTime * Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * moveForward);
transform.Translate(Vector3.right * moveLeft);
}
}
I suggest taking a look at Unity's Character Controller component as it is well helpful when moving the actual player. Also, main tutorials online for free provided by Unity to help you understand it more and really work with it at Unity's Demo Projects
Answer by Em3rgency · Jun 23, 2013 at 08:40 PM
Here's a snippet for running, if that's what you wanted? A one-time roll would essentially be the same, except you would need to add a timer to make pauses in between the fast movement.
//add this next to your variables.
int fastMultiplier = 2;
//This goes into the update function.
if(Input.GetKeyDown(KeyCode.LeftShift))
moveSpeed *= fastMultiplier;
if(Input.GetKeyUp(KeyCode.LeftShift))
moveSpeed /= fastMultiplier;
Answer by F3RILLA · Jul 24, 2013 at 01:52 PM
Thanks, that's a simpler way to make it run. I have worked on a new script now using Character Motor