Search code examples
c#visual-studiounity-game-enginevirtual-reality

How can I apply scripts in new camera which is child of a gameobject


Earlier I was facing problem regarding the unity camera problem it always stuck on 0,0,0.08 and also find a solution so I first create an empty gameobject and then drag the camera in that empty gameobject but after doing this the scripts which I applied to the gameobject is working fine but the script which I place in camera is not working at all

Camera Script

public float MovementAmplitude = 0.1f;
public float MovementFrequency = 2.25f;
void Update()
{
    transform.position = new Vector3(
         transform.position.x,
         Mathf.Cos(transform.position.z * MovementFrequency) * MovementAmplitude,
         transform.position.z

        );
}

Player Script

public float speed = 4.5f;
public float JumpingForcec = 450f;
void Update()
{
    transform.position += speed * Vector3.forward * Time.deltaTime;
    if (Input.GetKeyDown("space"))
    {
        Debug.Log("SPace is pressed");
        Debug.Log(GetComponent<Rigidbody>());
        GetComponent<Rigidbody>().AddForce(Vector3.up * JumpingForcec);
    }
}

Solution

  • First of all when dealing with a Rigidbody (or the Physics in general) you shouldn't set a position directly through the Transform component but rather use Rigidbody.position or in your case for a smooth movement even rather Rigidbody.MovePosition, both in FixedUpdate.

    In general anything related to the Physics (so also everything using Rigidbody) should be done in FixedUpdate while the check for GetKeyDown has to be done in Update.

    PlayerScript

    public class PlayerScript : MonoBehaviour
    {
        public float speed = 4.5f;
        public float JumpingForcec = 450f;
    
        // If possible reference this in the Inspector already
        [SerializeField] private Rigidbody rigidBody;
    
        private bool jumpWasPressed;
    
        private void Awake()
        {
            if (!rigidBody) rigidBody = GetComponent<Rigidbody>();
        }
    
        private void FixedUpdate()
        {
            rigidBody.MovePosition(transform.position + speed * Vector3.forward * Time.deltaTime);
    
            if (!jumpWasPressed) return;
    
            Debug.Log("SPace was pressed");
    
            rigidBody.AddForce(Vector3.up * JumpingForcec);
    
            jumpWasPressed = false;
        }
    
        private void Update()
        {
            // Note that currently you can multijump .. later you will want to add 
            // an additional check if you already jump again
            if (Input.GetKeyDown(KeyCode.Space)) jumpWasPressed = true;
        }
    }
    

    Make sure that Is Kinematic is disabled in the Rigidbody component! Otherwise AddForce is not processed.

    If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.

    The camera movement I would move to LateUpdate in order to make sure it is the last thing calculated after the other Update calls have finished. Especially after all user input has been processed (in your case maybe not that relevant since movement is processed in FixedUpdate but in general).

    Second problem: Here you are not taking the changed Y position by jumping into account so rather add the "wobbling" effect to the player's transform.position.y and rather use the localPosition for the Camera:

    public class CameraScript : MonoBehaviour
    {
        public float MovementAmplitude = 0.1f;
        public float MovementFrequency = 2.25f;
    
        // reference the player object here
        public Transform playerTransform;
    
        private float originalLocalPosY;
    
        private void Start()
        {
            if(!playerTransform) playerTransform = transform.parent;
            originalLocalPosY = transform.localPosition.y;
        }
    
        private void LateUpdate()
        {
            transform.localPosition = Vector3.up * (originalLocalPosY + Mathf.Cos(playerTransform.position.z * MovementFrequency) * MovementAmplitude);
        }
    }
    

    Maybe you want to disable the wobbling effect during a jump later, though ;)

    enter image description here