Question by
airjairj · Jun 12, 2017 at 08:14 AM ·
movementcharactercharacter movement
How do i move my character in a direction that is not based on the character facing direction?
Example: the player is looking forward, if he press right the character goes on the right, but when he turns(180_degrees) himself if he press right the character turn left because the direction is based on where is he looking, how do I solve this?? PLS HELP!!! Here is the script: public class Player : MonoBehaviour { //Var public float movementSpeed; public GameObject camera; public Gun theGun;
//Methods
void Update()
{
//Player segue mouse
Plane playerPlane = new Plane (Vector3.up, transform.position);
Ray ray = UnityEngine.Camera.main.ScreenPointToRay (Input.mousePosition);
float hitDist = 0.0f;
if(playerPlane.Raycast(ray, out hitDist))
{
Vector3 targetPoint = ray.GetPoint(hitDist);
Quaternion targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
targetRotation.x = 0;
targetRotation.z = 0;
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, 7f * Time.deltaTime);
}
//movimenti Player
if (Input.GetKey (KeyCode.W))
{
transform.Translate (Vector3.forward * movementSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S))
{
transform.Translate (Vector3.back * movementSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A))
{
transform.Translate (Vector3.left * movementSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.D))
{
transform.Translate (Vector3.right * movementSpeed * Time.deltaTime);
}
if (Input.GetMouseButtonDown(0))
{
theGun.isFiring = true;
}
if (Input.GetMouseButtonUp(0))
{
theGun.isFiring = false;
}
}
Comment