Skip to content

Instantly share code, notes, and snippets.

@walterpalladino
Created July 10, 2018 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterpalladino/3854c0df50775ae23a770cadb89bcbe6 to your computer and use it in GitHub Desktop.
Save walterpalladino/3854c0df50775ae23a770cadb89bcbe6 to your computer and use it in GitHub Desktop.
Adding Torso Aim to 3d Model
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AimingTorso : MonoBehaviour {
[SerializeField]
private Transform spine;
[SerializeField]
private float rotationScale = 1.0f;
[SerializeField]
private float clampMin = -60.0f;
[SerializeField]
private float clampMax = 60.0f;
private float horzMovement;
private float prevSpineZ;
void Awake()
{
prevSpineZ = spine.localEulerAngles.z; // Save initial X rotation of spine
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
horzMovement = Input.GetAxis("Mouse Y") * rotationScale; // Invert direction so that aiming down results look down!
}
void LateUpdate()
{
float newAngle = prevSpineZ + horzMovement;
if (newAngle > 180.0f) newAngle = newAngle - 360.0f;
Debug.Log(newAngle);
newAngle = Mathf.Clamp(newAngle, clampMin, clampMax);
spine.localRotation = Quaternion.Euler(
spine.localEulerAngles.x, // Calculate new x rotation by adding mouse movement and previous spine's X rotation
spine.localEulerAngles.y,
newAngle
);
prevSpineZ = spine.localEulerAngles.z; // Update previous x rotation to current rotation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment