Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help: Change player controls

Discussion in 'Scripting' started by Harbinger-Czar, Jun 25, 2018.

  1. Harbinger-Czar

    Harbinger-Czar

    Joined:
    Jun 25, 2018
    Posts:
    7
    I'm struggling with this one as well. I'm trying to get the player controls to rotate as the camera does. Any thoughts?

    Player Controller:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float speed;
    9.     bool isMoving;
    10.     float distance;
    11.     Vector3 endPos;
    12.  
    13.     void Start()
    14.     {
    15.         isMoving = false;
    16.         //rb = GetComponent<Rigidbody>();
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.  
    22.         if (Input.GetKey("left") && isMoving == false)
    23.         {
    24.             isMoving = true;
    25.             RaycastHit hit;
    26.             Ray leftRay = new Ray(transform.position, Vector3.left);
    27.  
    28.             if (Physics.Raycast(leftRay, out hit))
    29.             {
    30.                 if (hit.collider != null)
    31.                 {
    32.                     endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
    33.                 }
    34.             }
    35.         }
    36.  
    37.         if (Input.GetKey("right") && isMoving == false)
    38.         {
    39.             isMoving = true;
    40.             RaycastHit hit;
    41.             Ray rightRay = new Ray(transform.position, Vector3.right);
    42.             if (Physics.Raycast(rightRay, out hit))
    43.             {
    44.                 if (hit.collider != null)
    45.                 {
    46.                     endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
    47.                 }
    48.             }
    49.  
    50.         }
    51.  
    52.         if (Input.GetKey("up") && isMoving == false)
    53.         {
    54.             isMoving = true;
    55.             RaycastHit hit;
    56.             Ray upRay = new Ray(transform.position, Vector3.forward);
    57.             if (Physics.Raycast(upRay, out hit))
    58.             {
    59.                 if (hit.collider != null)
    60.                 {
    61.                     endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
    62.                 }
    63.             }
    64.  
    65.         }
    66.  
    67.         if (Input.GetKey("down") && isMoving == false)
    68.         {
    69.             isMoving = true;
    70.             RaycastHit hit;
    71.             Ray downRay = new Ray(transform.position, -Vector3.forward);
    72.             if (Physics.Raycast(downRay, out hit))
    73.             {
    74.                 if (hit.collider != null)
    75.                 {
    76.                     endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
    77.                 }
    78.             }
    79.  
    80.         }
    81.         distance = Vector3.Distance(transform.position, endPos);
    82.         //Debug.Log(distance);
    83.         if (distance > 0)
    84.         {
    85.             transform.position = Vector3.Lerp(
    86.                 transform.position, endPos,
    87.                 Time.deltaTime * speed / distance);
    88.             transform.rotation = Quaternion.identity;
    89.         }
    90.         if (distance == 0)
    91.         {
    92.             isMoving = false;
    93.             endPos = transform.position;
    94.         }
    95.  
    96.     }
    97.  
    98. }
    Camera Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PuzzleCamera : MonoBehaviour
    6. {
    7.     public void RotateLeft()
    8.     {
    9.         transform.Rotate(Vector3.up, 90, Space.Self);
    10.     }
    11.  
    12.     public void RotateRight()
    13.     {
    14.         transform.Rotate(Vector3.up, -90, Space.Self);
    15.     }
    16.    
    17. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you mean that you want to use the local "right" when you are rotated rather than the global "right" and so forth? If so, try using 'transform.right' instead of 'Vector3.right' for example.

    If you meant something else, please explain.
     
  3. Harbinger-Czar

    Harbinger-Czar

    Joined:
    Jun 25, 2018
    Posts:
    7
    So when you use the rotation buttons in the PuzzleCamera script, the camera will turn but the controls will stay the same. For example, when the camera turns right, the user will press down, but the player object moves left. Is there a way to change the orientation of the player object when the camera is turned?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you want it to be relative to the camera's rotation, you can use the camera's transform ".right / .up" (or "-.right / -transform.up). Try something like that.. :)