Search Unity

Rotate Player in given direction

Discussion in 'Scripting' started by SimonMK7, Jun 20, 2020.

  1. SimonMK7

    SimonMK7

    Joined:
    Sep 13, 2018
    Posts:
    4
    Hello, i'm currently creating a top-down 3D shooter project and am at the testing phase where I'm testing the player's movement and so far it seems to be working well. I've gotten the camera to follow the player and have also implemented a way to allow the player to be able to shoot (even though it might still need a little bit of work). I also wanted to add a way to be able to have the player press the ABXY controls on a gamepad in order to rotate the player object/mesh and face a specific direction without it affecting the current directional movement (ex. using the directional pad or key input press for movement and using the abxy buttons to rotate/face the player object in said direction). However, this is where I'm currently having trouble as so far I don't exactly know how to make a player face a specific direction.

    i've tryed using
    PlayerObj.transform.Rotate (Vector3.up * RotationSpeed * Time.deltaTime);
    but this only causes the PlayerObject to rotate around rather than a given direction.

    I'll go ahead and share the code that I have so that anyone can get a better idea of what I currently have.

    Edit: also apologies for my beginner-esque way of coding, I am just a beginner when it comes to Unity and am trying to further improve both in Unity and C# as a whole.

    --------------------------------------------------------------------------------------------------------------------------------------------

    Player script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.     public class Player : MonoBehaviour
    6.     {
    7.         [Header("Player Camera")]
    8.         [Tooltip("Select the camera to follow the player")]
    9.         public GameObject camera;
    10.  
    11.         [Header("Player Movement Values")]
    12.         [Tooltip("Main Player Object")]
    13.         public GameObject PlayerObj;
    14.  
    15.         [Tooltip("Player Obj Movement Speed")]
    16.         public float movementSpeed;
    17.  
    18.         [Tooltip("Player Mesh Rotation Speed")]
    19.         public float RotationSpeed;
    20.  
    21.         private Rigidbody rb;
    22.         private Vector3 direction;
    23.  
    24.         //public Vector3 Movement;
    25.  
    26.         [Header("Player Bullet Component")]
    27.         [Tooltip("Bullet Spawn Point Object")]
    28.         public GameObject bulletSpawnPoint;
    29.         public GameObject Bullet;
    30.         public float waitTime;
    31.         private Transform BulletSpawned;
    32.  
    33.         void Start()
    34.         {
    35.             rb = GetComponent<Rigidbody> ();
    36.         }
    37.  
    38.         void _Shoot()
    39.         {
    40.             BulletSpawned = Instantiate (Bullet.transform, bulletSpawnPoint.transform.position, Quaternion.identity);
    41.             BulletSpawned.rotation = bulletSpawnPoint.transform.rotation;
    42.         }
    43.  
    44.         void Movement()
    45.         {
    46.             float horizontalMove = Input.GetAxisRaw ("Horizontal");
    47.             float verticalMove = Input.GetAxisRaw ("Vertical");
    48.  
    49.             direction = new Vector3 (x: horizontalMove, y: 0.0f, z: verticalMove);
    50.  
    51.             if (direction != Vector3.zero)
    52.             {
    53.                 transform.rotation = Quaternion.Slerp (a: transform.rotation, b: Quaternion.LookRotation (direction), t: RotationSpeed * Time.deltaTime);
    54.             }
    55.             rb.MovePosition (transform.position + movementSpeed * Time.deltaTime * direction);
    56.         }
    57.  
    58.         // Update is called once per frame
    59.         void Update ()
    60.         {
    61.             Movement ();
    62.  
    63.             if (Input.GetKey (KeyCode.L) || (Input.GetKey (KeyCode.R)))
    64.             {
    65.                 _Shoot ();
    66.             }
    67.             /*if (Input.GetKey (KeyCode.A))
    68.             {
    69.             PlayerObj.transform.Rotate (Vector3.up * RotationSpeed * Time.deltaTime);
    70.             }*/
    71.         }
    72.     }
    --------------------------------------------------------------------------------------------------------------------------------------------

    PlayerCamera script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.     public class PlayerCamera : MonoBehaviour
    6.     {
    7.         [Header("Variables")]
    8.         public Transform player;
    9.         public float smooth = 0.3f;
    10.         public float height;
    11.  
    12.         private Vector3 velocity = Vector3.zero;
    13.      
    14.         // Update is called once per frame
    15.         void Update ()
    16.         {
    17.             Vector3 pos = new Vector3 ();
    18.             pos.x = player.position.x;
    19.             pos.y = player.position.y + height;
    20.             pos.z = player.position.z - 1f;
    21.  
    22.             transform.position = Vector3.SmoothDamp (transform.position, pos, ref velocity, smooth);
    23.         }
    24.     }
    --------------------------------------------------------------------------------------------------------------------------------------------

    Bullet script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.     public class Bullet : MonoBehaviour
    6.     {
    7.         [Header("Variables")]
    8.         public float Speed;
    9.  
    10.         public float maxDistance;
    11.      
    12.         // Update is called once per frame
    13.         void Update ()
    14.         {
    15.             transform.Translate (Vector3.forward * Time.deltaTime * Speed);
    16.             maxDistance += 1 * Time.deltaTime;
    17.              
    18.             if (maxDistance >= 2)
    19.                 Destroy (this.gameObject);
    20.         }
    21.     }
    22.  
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Something like this:

    Code (CSharp):
    1.     Vector3 lookAtPos = target.position - player.transform.position;
    2.     // lookAtPos.y = player.transform.position.y; // do not rotate the player around x
    3.     Quaternion newRotation = Quaternion.LookRotation(lookAtPos, player.transform.up);
    4.     player.transform.rotation = Quaternion.Slerp(player.transform.rotation, newRotation, Time.deltaTime * 8);
     
    Last edited: Jun 20, 2020
    bdotadeniyi likes this.
  3. SimonMK7

    SimonMK7

    Joined:
    Sep 13, 2018
    Posts:
    4
    @Zer0Cool apologies for asking but what does target do or what do you mean by it? i ask because it does not appear when using MonoDevelop and gives me an error when trying to use it as such. I should have mentioned that I'm using Unity 5.6 or 2017.1b for this project.
     
    Last edited: Jun 20, 2020
  4. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    target it the transform on what the player should look at ...

    But i noticed you get the input vector presumly from the ABXY controls. Here you must create a vector from these controls (for the direction like you have done in already for you movement vector)
    For instance user presses A then .x = 1 or when user presses B then .x = -1
    then user presses X then .y = 1 or when user presses Y then .y = -1
    (i dont know these ABXY controls so i only can guess here)

    and then use this code:
    Code (CSharp):
    1.  
    2.     Vector3 inputVector = inputVectorABXYcontrols.normalized; // here you must check that this vector in .xy axes is showing in world space in the direction you want (controled by the ABXY controls)!
    3.     inputVector.z = 0;
    4.     Debug.DrawLine(player.transform.position, player.transform.position + inputVector * 5f, Color.red, 0.5f); // Here some code to check the input vector in scene view
    5.     Quaternion lookRotation = player.transform.rotation;
    6.     Vector3 directionlookRotation = lookRotation * inputVector;
    7.     Quaternion newRotation = Quaternion.LookRotation(directionlookRotation, player.transform.up);
    8.     player.transform.rotation = Quaternion.Slerp(player.transform.rotation, newRotation, Time.deltaTime * 8);
    9.  
     
    Last edited: Jun 21, 2020
    SimonMK7 likes this.
  5. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    I updated the above code and description.
     
  6. SimonMK7

    SimonMK7

    Joined:
    Sep 13, 2018
    Posts:
    4
    ok thankyou, i'll make sure to try it and see if i can get it to work.