Question by
JsephRly · Jan 27, 2017 at 06:30 PM ·
c#rotationscripting problemcharacter
Retaining rotation (preventing rotation snapping)
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 10.0F;
public Transform ribcage;
public Transform pelvis;
private Vector3 moveDirection = Vector3.zero;
private Vector3 targetDirection = Vector3.zero;
void Update()
{
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
Moving (h, v);
Rotating (h, v);
}
void Moving(float horin, float verti) {
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(horin, 0, verti);
moveDirection = transform.TransformVector(moveDirection);
moveDirection *= speed;
controller.Move(moveDirection * Time.deltaTime);
}
void Rotating(float horin, float verti)
{
targetDirection = new Vector3(-horin, ribcage.eulerAngles.x, -verti);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
ribcage.rotation = targetRotation;
}
In Rotation I'm referencing bones instead of the entire charater since I'm trying to make a quadruped that moves like this; I keep having a problem with where the Z axis rotation of ribcage keeps snapping to -180 whenever I'm not holding down directional keys. I think this is a problem with ribcage.rotation = targetRotation;, but I can't find a way to change the angle so it retains targetRotation.
I've referenced the API character controller.move script for the Move method and the youtube unity stealth tutorial for the rotation method.
Comment