Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug player/camera rotation problems

Discussion in 'Scripting' started by unity_211781, May 11, 2023.

  1. unity_211781

    unity_211781

    Joined:
    Mar 29, 2023
    Posts:
    1
    hi i'm trying to make my player rotate when my camera rotates so when I go forwords it goes in the direction the camera is pointed here is my code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class playercontroler : MonoBehaviour
    {

    public float horizontalInput;
    public float verticalInput;
    public float speed = 20;



    public bool isGrounded;
    public Rigidbody rb;
    public Quaternion rotate;
    public GameObject camera;
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()

    {

    horizontalInput = Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);
    verticalInput = Input.GetAxis("Vertical");
    transform.Translate(Vector3.forward * verticalInput * Time.deltaTime * speed);
    rotate = camera.transform.rotation;

    transform.rotation = rotate;
    }
    void OnCollisionStay()
    {
    isGrounded = true;


    }
    }