- Home /
How do I move a Mecanim driven character relative to the camera?
I'm trying to create a camera controller and character motor pair so that the game behaves similar to Kingdoms of Amalur. The example controller in Mecanim has WASD moving the avatar relative to itself (and then the camera follows the avatar). Instead I want WASD to move the avatar relative to the camera, which is controlled with the mouse. So W always moves the avatar away from the camera, and S always moves the avatar toward the camera. I have the camera controller working (code below). But I am having problems modifying the animation graph and implementing a motor to do so. Can somebody please help? Thanks in advance.
using UnityEngine;
using System.Collections;
public class ThirdPersonCamera : MonoBehaviour
{
public GameObject _lookAtTarget;
public Vector3 _lookAtOffset;
public float _minPitchAngle = -20f;
public float _maxPitchAngle = 30f;
public float _sensitivity = 5f;
public float _springStiffness = 0f;
void LateUpdate ()
{
Vector3 eulerRotation = transform.localEulerAngles;
bool useController = (Input.GetJoystickNames().Length > 0);
eulerRotation.y += (useController ? Input.GetAxis("Joystick X") : Input.GetAxis("Mouse X")) * _sensitivity;
eulerRotation.x -= (useController ? Input.GetAxis("Joystick Y") : Input.GetAxis("Mouse Y")) * _sensitivity;
if (eulerRotation.x > 180f)
{
eulerRotation.x -= 360f;
}
eulerRotation.x = Mathf.Clamp(eulerRotation.x, _minPitchAngle, _maxPitchAngle);
Quaternion rotation = Quaternion.Euler(eulerRotation);
transform.position = _lookAtTarget.transform.position - (rotation * _lookAtOffset);
transform.LookAt(_lookAtTarget.transform);
}
}
have you figured this out? i did it using ThirdPersonController scripting but now I can't port that to mecanim, I don't really know what I need to do
Answer by nrvllrgrs · Feb 24, 2013 at 04:26 PM
Here's my rough version right now. It's not quite right yet, but it's closer than it was before. If you have any revisions, please do share.
using UnityEngine;
using System.Collections;
public class IdleRunJump : MonoBehaviour
{
private float _minSpeedThreshold = 1.55f;
private float _maxSpeedThreshold = 5.5f;
private float _maxAngularThreshold = 132.9878f;
public float _speedDampTime = 0.25f;
public float _angularSpeedDampTime = 0.25f;
public bool ApplyGravity = true;
protected Animator _animator;
private int _speedId;
private int _angularSpeedId;
private int _directionId;
private int _jumpId;
// Use this for initialization
void Start ()
{
_animator = GetComponent<Animator>();
if(_animator.layerCount >= 2)
_animator.SetLayerWeight(1, 1);
_speedId = Animator.StringToHash("Speed");
_angularSpeedId = Animator.StringToHash("AngularSpeed");
_directionId = Animator.StringToHash("Direction");
_jumpId = Animator.StringToHash("Jump");
}
// Update is called once per frame
void Update ()
{
if (_animator)
{
AnimatorStateInfo stateInfo = _animator.GetCurrentAnimatorStateInfo(0);
bool useController = (Input.GetJoystickNames().Length > 0);
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 forwardAvatar = new Vector3(transform.forward.x, 0f, transform.forward.z).normalized;
Vector3 forward = Camera.mainCamera.transform.TransformDirection(Vector3.forward);
forward.y = 0f;
forward = forward.normalized;
Vector3 right = new Vector3(forward.z, 0f, -forward.x);
Vector3 direction = ((horizontal * right) + (vertical * forward)).normalized;
float deltaAngle = Vector3.Angle(direction, forwardAvatar);
float dot = Vector3.Dot(forwardAvatar, Vector3.Cross(direction, Vector3.up));
_animator.SetFloat(
_speedId, ((horizontal*horizontal) + (vertical*vertical)) * (!useController && Input.GetButton("Walk") ? _minSpeedThreshold : _maxSpeedThreshold), _speedDampTime, Time.deltaTime
);
float headingOffset = deltaAngle * Mathf.Sign(dot);
_animator.SetFloat(_angularSpeedId, headingOffset, _angularSpeedDampTime, Time.deltaTime);
_animator.SetFloat(_directionId, headingOffset);
}
}
}
Your answer
Follow this Question
Related Questions
Camera follow Avatar 1 Answer
rotate Y axis to face mouse 1 Answer
Player walks to the right? 1 Answer
How to add Pinch to Zoom? 0 Answers
Set Max Rotation On Weapon Sway 0 Answers