Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question First Person Script

Discussion in 'Scripting' started by Corrothon, Aug 16, 2020.

  1. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Hi So I'm working on this Horror game and it's not that big but I have some problems with the Player Script. I wanted some help on how I could make the player rotate.
    Here's my script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Playermovement : MonoBehaviour {
    public float moveSpeed;

    // Use this for initialization
    void Start()
    {
    moveSpeed = 15f;
    }

    // Update is called once per frame
    void Update()
    {
    transform.Translate(moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f,moveSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Since you haven't tried anything yet, here's an excellent tutorial on the subject:
     
  3. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Ýeah but people in the comments are saying that it's not working for them.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Not everyone is 100% capable of following instructions ;D. Have you tried it for yourself? Are you running into some specific problem?
     
  5. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Nope haha but I think it depends on the Unity Version idk they update things lol
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    That tutorial is less than a year old. I assure you none of the things in that video have changed in Unity. I'll be glad to help you further if you try it out and run into problems.
     
  7. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Alright thank you :)
     
  8. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    So There's a problem with the rotation.
    Here's my Mouse Script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MouseScript : MonoBehaviour
    {

    public float mouseSensitivity = 100f;

    public Transform playerBody;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    playerBody.Rotate(Vector3.up * mouseX);
    }
    }
     
  9. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Nvm Just figured it out haha
     
  10. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Okay so I did the rotation script and I'm now on the Key script but everytime I try to move with WASD it just flies up into the air
    Here's my script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovementScript : MonoBehaviour
    {
    public CharacterController controller;

    public float speed = 12f;


    // Update is called once per frame
    void Update()
    {
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);
    }
    }
     
  11. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Okay and Now the player has epileptic seizures
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Do you have any other scripts attached to this object or its parent or children? For example your original movement script from your first post?
     
  13. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Nope here's some screenshot's:
     

    Attached Files:

  14. Corrothon

    Corrothon

    Joined:
    Jun 11, 2019
    Posts:
    60
    Oh It was the capsule collider not set on trigger haha
     
  15. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    If you're following the tutorial I shared there shouldn't be other colliders or Rigidbody on the character at all.