Question by
JPercy · Feb 23, 2017 at 06:20 PM ·
movementrotate objectwalking
character turning full circle
Hey! Help?! please!
I've managed to script my character to finally rotate and move so left means the character rotates left, etc ....but the keys remain the same as oppose to updating. So when I turn right to go along the +z axis if I press right again the character remains along the +z axis...as oppose to rotating again along what would be the -x axis - so I need the x,y,z axis to rotate with the character? How would I do that? would it be a case of changing settings to do with local or world axis? Any help would be appreciated!
This is the Code I've got so far:
using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLength = 100f;
void Awake ()
{
floorMask = LayerMask.GetMask ("Floor");
anim = GetComponent <Animator> ();
playerRigidbody = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h, v);
Turning ();
Animating (h, v);
}
void Move (float h, float v)
{
movement.Set (h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
}
void Turning ()
{
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
playerRigidbody.MoveRotation (newRotation);
}
}
void Animating (float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool ("IsWalking", walking);
}
void Update ()
{
if (movement != Vector3.zero)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.normalized), 0.2f);
}
}
Comment