- Home /
Making a non player character move forward
How can I animate a character to constantly move in some vector3 direction, I know hot to make a mecanim character controller / how to make it move forward
/// This script moves the character controller forward
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to Move or SimpleMove per frame.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
I have been thinking about something like that:
transform.TransformDirection(Vector3.up * Time.deltaTime, Space.World);
Maybe there is some easier solution to this, can't come up with anything
Answer by growling_egg · Jul 22, 2013 at 10:51 PM
Are you looking for something more complex than this?
var speed : int = 1; // Adjust to make your NPC move however fast you want.
function Update ()
{
transform.Translate(Vector3(forward) * speed * Time.deltaTime);
}
using UnityEngine;
using System.Collections;
public class movement : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
int speed = 1; // Adjust to make your NPC move however fast you want.
void Update ()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
$$anonymous$$y compiler went rogue and did not want to compile anything lol, I can only do C# scripts. That is the outcome and it is the solution to my problem, Thanks!
Answer by fransh · Jul 22, 2013 at 10:48 PM
If you just want it to move forward, and you want it to move the walk speed given in that var speed, simply put down:
transform.Translate(Vector3.forward speed Time.deltaTime);
Answer by IgorAherne · Jul 22, 2013 at 10:06 PM
you can move anything that has a js script attached. In that script, delete anything and write this:
function FixedUpdate(){
transform.Translate(0,0,0.2); //this moves an object that has this script forward along z axis
}
//fixedUpdate is by default every 0.02 seconds so your character will be moving 10 unsits a sec
you could also do
function Update(){
transform.Translate(0,0,10)*Time.deltaTime;
}