Question by
Abrakidabra · May 28, 2017 at 03:14 PM ·
scripting beginner
Rotate Upperbody to camera direction
Im a beginner unity user and Im struggling to rotate the spine bone to the camera direction so my character can aim upwards or downwards without his feet leaving the ground. Here is my code but its not working
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move : MonoBehaviour {
float lockPos = 0;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float forward = Input.GetAxis ("Vertical"); //Walk forward input
float strafe = Input.GetAxis ("Horizontal"); // Walk Sideways input
anim.SetFloat ("Forward", forward); //Animate forward
anim.SetFloat ("Strafe", strafe); // Animate Sideways
transform.forward = Camera.main.transform.forward; //Make player look where the camera is looking
transform.rotation = Quaternion.Euler (lockPos, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z); // lock players x axis rotation when aiming vertically so feet stay on ground
}
void LateUpdate () {
transform.Find("LowManSpine").forward = Camera.main.transform.forward; // attempt to rotate players upper body to face cameras vertical aim (But it does nothing)
}
}
Not sure why its not working, getting a null refernce execption but I think there will be more issues even if I solved that. Any ideas to get it to work? Thanks in advance!
Comment