public class Controller : MonoBehaviour { // Made by HEWKEW
public float speed = 4;
private CharacterController controller;
private void Start() {
//We need a controller. This will let us access to very useful functions.
controller = GetComponent<CharacterController>();
}
private void Update() {
//We start our functions and keep them running.
Move();
Rotate();
}
private void Move()
{
//We need a Vector3 to storage our input horizontal for our x axis and vertical for our z axis. The y axis
//Is not needed because we won't move our object vertically.
Vector3 movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
//Since movement has inputs with maximun 1 and minimun -1, we multiply it to our speed. For example:
//Speed: 4 ==> Movement (1, 0, or -1) * Speed = (4, 0, -4)
controller.SimpleMove(speed * movement);
}
private void Rotate()
{
//Ray from the main camera to the mouse position.
Ray _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//New plane (not a real plane) to get the raycast.
Plane _plane = new Plane(Vector3.up, Vector3.zero);
//Distance from the center of the plane to the raycast (position 0 + distance from it to the hit)
float distance;
if(_plane.Raycast(_ray, out distance))
{
//We wont need to rotate our character to look to the ground. So we need a new Vector3 to ignore it.
Vector3 point = new Vector3(_ray.GetPoint(distance).x, transform.position.y, _ray.GetPoint(distance).z);
//Then, we look at that point.
transform.LookAt(point);
}
}
}
Replies to Movement TDS Simple Scripting
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}