Rotate an object diagonally and prevent it from going back to 90,180,0,360 degrees
After holding down on a combination keys of AS,SD,DW or WA to move my object, it will successfully move diagonally and rotate to the correct position, but after releasing the keys, it will rotate back to either 0,90,180 or 360 depending on the nearest rotation after releasing my keys, I guess it's because of that 1 frame that I left the keys touched, so it ran that code and moved it to 0,90,180,360. But I don't know how to solve it.
It's my first time using unity answers, but I hope I've provided enough information, and thanks for helping out.
I'm pressing onto AW keys together
After releasing the keys, it doesn't stay that way but moves to either left or top PlayerMovement.cs
using UnityEngine;
using System.Collections;
public class playerMovement : MonoBehaviour {
private float speedWalk = 7.5f;
private float speedRotate = 7.5f;
private GameObject raycastObject;
// Update is called once per frame
void FixedUpdate ()
{
//Non-Diagonal Movements
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.D)) //player movement up
{
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.D)) //player movement down
{
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement right
{
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) //player movement left
{
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
//Diagonal Movements **@@@@@@@@** I think this is the problem.
if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W)) //player movement Top Left
{
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) //player movement Top Right
{
transform.localPosition += new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S)) //player movement Bottom Right
{
transform.localPosition += new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) //player movement Bottom Left
{
transform.localPosition -= new Vector3(0.0f, 0.0f, speedWalk * Time.deltaTime);
transform.localPosition -= new Vector3(speedWalk * Time.deltaTime, 0.0f, 0.0f);
}
}
}
playerRotateMouse.cs
using UnityEngine;
using System.Collections;
public class playerRotateMouse : MonoBehaviour
{
public Transform Player;
private float speed = 7.5f;
Quaternion targetRotation;
void FixedUpdate()
{
Plane playerPlane = new Plane(Vector3.up, transform.position);
// Generates a ray from cursor
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (Input.GetButtonDown("Fire1") || Input.GetButtonDown("Fire2"))
{
if (playerPlane.Raycast(ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
Debug.DrawRay(transform.position, targetPoint, Color.green);
targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
}
}
// Smooth rotation towards point.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
//Rotate base on key pressed
if (Input.GetKey(KeyCode.W) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate up
{
targetRotation = Quaternion.LookRotation(Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate down
{
targetRotation = Quaternion.LookRotation(Vector3.forward * -1);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate right
{
targetRotation = Quaternion.LookRotation(Vector3.right);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) && !(Input.GetButton("Fire1") || Input.GetButton("Fire2"))) //player rotate left
{
targetRotation = Quaternion.LookRotation(Vector3.left);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}
Your answer
Follow this Question
Related Questions
Applying AddForce() while rotating player gameobject. 0 Answers
Cannot rotate over 360 degrees 1 Answer
Why the subtracting compound assignment operator is used to ADD a angle in unity? 1 Answer
Reset rotation of gameobject 1 Answer
have enemy try to get in front of player and push them back help 0 Answers