- Home /
Solved it myself
Character Controller Move in X and Z axis via camera
Hello so I'm trying to move my thirdperson character controller relative to the camera's direction... It works, but however it moves on it's own and also moves on the Y-Axis if looking upwards or downwards. How can I fix this so that it doesn't move up or down?
TP_Motor Script for Movement
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour
{
public static TP_Motor Instance;
public Vector3 camForward;
public float MoveSpeed = 10f;
public Vector3 MoveVector { get; set; }
void Awake()
{
Instance = this;
}
public void UpdateMotor() //not update since the function needs to be called on manually
{ //when UpdateMotor is called, call these functions as well
//SnapAlign();
ProcessMotion();
RotateMotion();
}
void ProcessMotion() // to move the direction of the model's direction i.e strafing
{
// Transform MoveVector to Worldspace
MoveVector = transform.TransformDirection(MoveVector);
// Normalize MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed;
// Multiply MoveVector by DeltaTime
MoveVector *= Time.deltaTime;
// Move in World Space
TP_Controller.CharacterController.Move(MoveVector);
//MoveVector = Camera.main.transform.TransformDirection(MoveVector);
}
void SnapAlign() //a function used to snap the character forwards.. i don't use this for the time being
{
if (MoveVector.x != 0 || MoveVector.z != 0)
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
Camera.main.transform.eulerAngles.y,
transform.eulerAngles.z);
}
}
void RotateMotion() //This is to move direction relative to the camera
{
// Transform MoveVector to Worldspace
MoveVector = Camera.main.transform.TransformDirection(Vector3.forward);
// Normalize MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed;
// Multiply MoveVector by DeltaTime
MoveVector *= Time.deltaTime;
// Move in World Space
TP_Controller.CharacterController.Move(MoveVector);
//MoveVector = Camera.main.transform.TransformDirection(MoveVector);
}
}
ThirdPerson_Controller Script for handling inputs
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour {
//note that a static variable is an unchanging variable that isn't manipulated.
public static CharacterController CharacterController;
public static TP_Controller Instance;
void Awake()
{
CharacterController = GetComponent("CharacterController") as CharacterController;
Instance = this;
TP_Camera.ExistingOrNewCamera();
}
void Update()
{
if (Camera.main == null)
return;
GetLocomotionInput();
TP_Motor.Instance.UpdateMotor();
}
void GetLocomotionInput()
{
var deadZone = 0.1f;
TP_Motor.Instance.MoveVector = Vector3.zero;
if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
}
TP_Camera Script for the Camera using UnityEngine; using System.Collections;
public class TP_Camera : MonoBehaviour
{
public static TP_Camera Instance;
public Transform TargetLookAt;
public float Distance = 15f;
public float DistanceMin = 10f;
public float DistanceMax = 20f;
public float DistanceSmooth = 0.05f;
public float X_MouseSensitivity = 5f;
public float Y_MouseSensitivity = 5f;
public float WheelSensitivity = 15f;
public float X_Smooth = 0.05f;
public float Y_Smooth = 0.01f;
public float Y_MinLimit = -40f;
public float Y_MaxLimit = 80f;
private float mouseX = 0f;
private float mouseY = 0f;
private float velX = 0f;
private float velY = 0f;
private float velZ = 0f;
private float velDistance = 0f;
private float startDistance = 0f;
private float desiredDistance = 0f;
private Vector3 position = Vector3.zero;
private Vector3 desiredPosition = Vector3.zero;
void Awake()
{
Instance = this;
}
void Start ()
{
Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax); //Set Distance's distance minimum and max distance
startDistance = Distance;
Reset();
}
void LateUpdate ()
{
if (TargetLookAt == null)
return;
HandlePlayerInput();
DesiredPosition();
UpdatePosition();
}
void HandlePlayerInput()
{
var deadZone = 0.01f;
//if(Input.GetMouseButton(1))
{
// RMB is down get mouse Axis input
mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
}
// Limit mouseY
mouseY = Helper.ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit); //send the helper clamp angle into our mouseY
if(Input.GetAxis("Mouse ScrollWheel") < -deadZone
|| Input.GetAxis("Mouse ScrollWheel") > deadZone)
{
desiredDistance = Mathf.Clamp(Distance - Input.GetAxis("Mouse ScrollWheel") *
WheelSensitivity, DistanceMin, DistanceMax);
}
}
void DesiredPosition()
{
//Evaluate distance
Distance = Mathf.SmoothDamp(Distance, desiredDistance,
ref velDistance, DistanceSmooth);
//Calculate desired position
desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
}
Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
{
Vector3 direction = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
return TargetLookAt.position + rotation * direction;
}
void UpdatePosition()
{
var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
position = new Vector3(posX, posY, posZ);
transform.position = position;
transform.LookAt(TargetLookAt);
}
public void Reset()
{
mouseX = 0;
mouseY = 10;
Distance = startDistance;
desiredDistance = Distance;
}
public static void ExistingOrNewCamera() //Checking for a main camera..
//If one isn't there it makes a new one
{
GameObject tempCamera;
GameObject targetLookAt;
TP_Camera myCamera;
if (Camera.main != null)
{
tempCamera = Camera.main.gameObject;
}
else
{
tempCamera = new GameObject("Main Camera");
tempCamera.AddComponent<Camera>();
tempCamera.tag = "MainCamera";
}
tempCamera.AddComponent<TP_Camera>();
myCamera = tempCamera.GetComponent<TP_Camera>() as TP_Camera;
targetLookAt = GameObject.Find("targetLookAt") as GameObject;
if(targetLookAt == null)
{
targetLookAt = new GameObject("targetLookAt");
targetLookAt.transform.position = Vector3.zero;
}
myCamera.TargetLookAt = targetLookAt.transform;
}
}
Worth noting the Rotate$$anonymous$$otion() is the one i need help with.
Answer by AgonyRoses · Aug 07, 2016 at 10:07 AM
Never mind solved it myself. I had to add some extra variables:
public Vector3 camForward;
public Transform cam;
and change RotateMotion() into this:
void RotateMotion() //This is to move direction relative to the camera {
cam = Camera.main.transform;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
MoveVector = (v * camForward + h * cam.right).normalized;
MoveVector *= Time.deltaTime;
MoveVector *= MoveSpeed;
TP_Controller.CharacterController.Move(MoveVector);
}
So now I have a function for moving relative to the camera and the other for moving relative to the direction faced.