Hello, I'm attempting to build a character controller script that will control a bipedal generic rig I've created. So far, the script I've built, based of the one in this tutorial series: https://www.youtube.com/watch?v=DEqBzTcuEVw can make my character move forward, backward, strafe left or right, strafe diagonally forward or backward and lastly, turn while running. My current issue is that while the character is turning and running, it slides around the turn, kind of facing outward. What I would like is to have the character facing into the direction that it is turning. I've looked into quaternions and some of the associated functions but haven't been able to figure anything out. Anyway, I'm all out of ideas and any suggestions are greatly appreciated. Thanks for taking a look...
Unit.cs:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class Unit : MonoBehaviour
{
protected CharacterController control;
protected Vector3 move = Vector3.zero;
public float walkSpeed = 5f;
public float runSpeed = 10f;
public float turnSpeed = 130f;
public float jumpSpeed = 10f;
protected bool jump;
protected bool running;
protected Vector3 gravity = Vector3.zero;
// Use this for initialization
public virtual void Start()
{
control = GetComponent();
if (!control)
{
Debug.LogError("Unit.Start() " + name + " has no CharacterController");
enabled = false;
}
}
// Update is called once per frame
public virtual void Update()
{
//control.SimpleMove (move * moveSpeed); //takes movement deltas in meters per second (don't use delta time)
if (running)
move *= runSpeed;
else
move *= walkSpeed;
if (!control.isGrounded)
{
gravity += Physics.gravity * Time.deltaTime;
}
else
{
gravity = Vector3.zero;
if (jump)
{
gravity.y = jumpSpeed;
jump = false;
}
}
move += gravity;
control.Move(move * Time.deltaTime); // uses absolute movement deltas (use delta time)
}
}
UnitPlayer.cs:
using UnityEngine;
using System.Collections;
public class UnitPlayer : Unit
{
int goSpeed = 0;
// Use this for initialization // MECH CODE
public override void Start()
{
base.Start();
}
// Update is called once per frame
public override void Update()
{
//transform.Rotate(0f, Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime, 0f); // turn using mouse
transform.Rotate(0f, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0f); // turn using 'A' and 'D' keys
if (Input.GetAxis("Vertical") == 0) // turn on a time while not moving forward
{
move = Vector3.zero;
}
else
{
move = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
}
if (Input.GetKey(KeyCode.E))
{
move += Vector3.right;
}
if (Input.GetKey(KeyCode.Q))
{
move += Vector3.left;
}
move.Normalize();
move = transform.TransformDirection(move);
Debug.Log("move. x " + move.x);
Debug.Log("move. y " + move.y);
Debug.Log("move. z " + move.z);
if (Input.GetKeyDown(KeyCode.Space) && control.isGrounded)
{
jump = true;
}
running = Input.GetKey(KeyCode.LeftShift);
base.Update();
}
}
↧