- Home /
My player movement rotation script is acting "up"
I have a basic player movement script where the player orients based on his movement, and he totally does face the way he is moving. awesome. however when he isn't moving he returns to face "up" or like north. not awesome.
so how would i fix this? his rotations are frozen on the rigidbody, and i tried kicking up the drag/angular drag super high. i do get a console error when i play saying "Look rotation viewing vector is zero. Unity.engine.Quaternion:LookRotation(vector3,vector3)." which then points to my 4th line of the rotation script.
but I'm not a smart coding man please help D:
also here's the rotation portion of the script.
void Rotating (float horizontal, float vertical)`enter code here`
{
Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up );
Quaternion newRotation = Quaternion.Lerp (rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
rigidbody.MoveRotation (newRotation);
}
right, mybad. i guess its also worth noting that i kinda frankenstein'd two movement scripts together to make this lol.
here it is the whole script:
using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float speed = 12f;
public float turnSmoothing = 15f;
public float speedDampTime = 0.1f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
//int floor$$anonymous$$ask;
//float camRayLength = 100f;
void Awake ()
{
//floor$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask ("Floor");
anim = GetComponent <Animator> ();
playerRigidbody = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
$$anonymous$$ove (h, v);
Rotating (h, v);
Animating (h, v);
}
void $$anonymous$$ove (float h, float v)
{
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.$$anonymous$$ovePosition (transform.position + movement);
}
void Rotating (float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up );
Quaternion newRotation = Quaternion.Lerp (rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
rigidbody.$$anonymous$$oveRotation (newRotation);
}
void Animating (float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool ("IsWalking", walking);
}
}
Answer by ElDo · Jan 20, 2015 at 05:30 AM
the error you get is absolutely true. You'll get Vector3(0,0,0) which is equal to Vector3.zero if you stop moving in any direction. So have you tried to Change that Piece of code to maybe transform.forward or Vector3(0,0,1) as soon as both horizontal and vertical Input are 0? like that:
Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
if(horizontal==0 && vertical==0)targetDirection=new Vector3(0,0,1);
Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up );
Shoot, i never tried that. it doesn't quite seem to work. but its a step forward and now i know where to look.
Thanks ElDo
Your answer

Follow this Question
Related Questions
How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers
Itween 180º curve position and rotation problem 0 Answers
reference variables within my xml file. 0 Answers
Rigidbody rotation: Use hinge joint motor or MoveRotation script? 0 Answers
How to Get a Script Component and call a function from another script? 0 Answers