Advertisement
LeeMace

Car movement & camera switch

Nov 29th, 2022 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | Gaming | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5.     [SerializeField] float speed = 15.0f;
  6.     [SerializeField] float turnSpeed = 35.0f;
  7.     private float horizontalInput;
  8.     private float forwardInput;
  9.  
  10.     //camera switch
  11.     public Camera mainCamera;
  12.     public Camera hoodCamera;
  13.     public KeyCode switchKey;
  14.  
  15.     //2 player
  16.     public string inputID;
  17.  
  18.     void FixedUpdate()
  19.     {
  20.         //player input
  21.         //control left/right movement
  22.         horizontalInput = Input.GetAxis("Horizontal" + inputID);
  23.         //control forward and back movement
  24.         forwardInput = Input.GetAxis("Vertical" + inputID);
  25.  
  26.         //Move the vehicle forward
  27.         transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
  28.         //have vehicle rotate as it turns
  29.         transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);
  30.  
  31.         if (Input.GetKeyDown(switchKey))
  32.         {
  33.             mainCamera.enabled = !mainCamera.enabled;
  34.             hoodCamera.enabled = !hoodCamera.enabled;
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement