- Home /
Player Rotation Snapping
Hello,
I have been following the evac city tutorial in my attempt to make a top down shooter. I have run into a snag when trying to use the Right analog stick on my logitech gamepad to determine player rotation. I can get the player to rotate with the stick just fine, but when I release the stick my player snaps back to looking towards the top of the screen and Unity gives a Log entry of "Look rotation vector is equal to zero". Code is below, any ideas are greatly appreciated.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private GameObject objPlayer;
private GameObject objCamera;
private Vector3 inputRotation;
private Vector3 inputMovement;
public float moveSpeed = 100.0f;
private bool thisIsPlayer;
private Vector3 tempVector;
private Vector3 tempVector2;
// Use this for initialization
void Start () {
objPlayer = (GameObject) GameObject.FindWithTag("Player");
objCamera = (GameObject) GameObject.FindWithTag("MainCamera");
if (gameObject.tag == "Player") { thisIsPlayer = true; }
}
// Update is called once per frame
void Update () {
FindInput();
ProcessMovement();
if (thisIsPlayer == true) {
HandleCamera();
}
}
void FindInput () {
if (thisIsPlayer == true)
{
FindPlayerInput();
}
}
void FindPlayerInput () {
inputMovement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") );
inputRotation = new Vector3(Input.GetAxis("RightH"), 0, Input.GetAxis("RightV") );
}
void ProcessMovement () {
tempVector = rigidbody.GetPointVelocity(transform.position) * Time.deltaTime * 1000;
rigidbody.AddForce(-tempVector.x, -tempVector.y, -tempVector.z);
rigidbody.AddForce(inputMovement.normalized * moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 180, 0);
transform.position = new Vector3(transform.position.x, 0, transform.position.z);
}
void HandleCamera () {
objCamera.transform.position = new Vector3(transform.position.x, 15, transform.position.z);
objCamera.transform.eulerAngles = new Vector3(90, 0, 0);
}
}</code>
Answer by robertbu · Jul 15, 2013 at 03:05 PM
The vector you pass to LookRotation() cannot be Vector3.zero. You can solve this error assuring that the vector passed is not zero. So line 101 becomes:
if (inputRotation != Vector3.zero)
transform.rotation = Quaternion.LookRotation(inputRotation);
Your answer
Follow this Question
Related Questions
need 2d object to face the angle of the analog stick 0 Answers
Rotation with Character Controller & Rotation using vector3 1 Answer
Best way to prevent unnecessary input handling? 1 Answer
How to smoothly rotate object in direction of analog joystick? 1 Answer
Make Child face direction of analog RELATIVE to parent's direction 0 Answers