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

How can I jump in First Person Camera ? Activating RigidBody mess things up

Discussion in 'Scripting' started by unity_FCiLP2Ol8HfqHg, Feb 14, 2018.

  1. unity_FCiLP2Ol8HfqHg

    unity_FCiLP2Ol8HfqHg

    Joined:
    Feb 14, 2018
    Posts:
    2
    Hi there !

    I'm very new to unity and this is my first message.
    I just started thinking I would love to make a simple game for myself and absolutely wanted to have it in first person. After following tutorials to get a nice first person movement and camera, I tried to implemente some code in order to jump but didn't succeed... It seems that people activate 'rigid body' but when I do so, even if I turn on 'freeze rotation x and z' my capsule stills 'get crazy'.

    So I was thinking if there was an other way to make my character jump.
    I'm sorry to come and just ask you guys, but I really tried for hours, following tutorials and stuff, and there is always something going wrong.
    Thanks in advance.

    Here are my two scripts if that can help :


    Playermove

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;



    public class PlayerMove : MonoBehaviour

    {

    CharacterController charControl;

    public float walkSpeed;



    void Awake()

    {

    charControl = GetComponent<CharacterController>();





    }



    void Update()

    {

    MovePlayer();



    }



    void MovePlayer()

    {

    float horiz = Input.GetAxis("Horizontal");

    float vert = Input.GetAxis("Vertical");



    Vector3 moveDirSide = transform.right * horiz * walkSpeed;

    Vector3 moveDirForward = transform.forward * vert * walkSpeed;



    charControl.SimpleMove(moveDirSide);

    charControl.SimpleMove(moveDirForward);

    }

    }




    Playerlook


    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;



    public class PlayerLook : MonoBehaviour

    {

    public Transform playerBody;

    public float mouseSensitivity;



    float xAxisClamp = 0.0f;



    void Awake()

    {

    Cursor.lockState = CursorLockMode.Locked;

    }

    void Update()

    {

    RotateCamera();

    }



    void RotateCamera()

    {

    float mouseX = Input.GetAxis("Mouse X");

    float mouseY = Input.GetAxis("Mouse Y");





    float rotAmountX = mouseX * mouseSensitivity;

    float rotAmountY = mouseY * mouseSensitivity;



    xAxisClamp -= rotAmountY;



    Vector3 targetRotCam = transform.rotation.eulerAngles;

    Vector3 targetRotBody = playerBody.rotation.eulerAngles;





    targetRotCam.x -= rotAmountY;

    targetRotCam.z = 0;

    targetRotBody.y += rotAmountX;



    if(xAxisClamp > 90)

    {

    xAxisClamp = 90;

    targetRotCam.x = 90;



    }

    else if(xAxisClamp < - 90)

    {

    xAxisClamp = -90;

    targetRotCam.x = 270;

    }





    transform.rotation = Quaternion.Euler(targetRotCam);

    playerBody.rotation = Quaternion.Euler(targetRotBody);









    }



    }
     
  2. unity_FCiLP2Ol8HfqHg

    unity_FCiLP2Ol8HfqHg

    Joined:
    Feb 14, 2018
    Posts:
    2
    No need to reply actually, just found a way using Character controller features !