Unity rigid body motion

nananana·2022년 9월 23일
0

Tried motion with rigid body
Now rotation does not screw up controls :D

Youtube Link
As always thank you creator.

Interesting problem related with update frame and fixed frame.
My update frame runs way faster than the fixed frame.
turns out the video was able to jump when placing key input in the fixed frame.

But mine didn't seem to respond quite well.
Makes sense (update() and FixedUpdate() run at different intervals like two threads but one runs more often) but actually no it doesn't

Still interesting concept to consider.

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

public class BallsJustBalls : MonoBehaviour
{
    Rigidbody rb;
    bool JumpPressed;
    bool isGround;
    bool isSpin;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        // rb.velocity = Vector3.right;
        // rb.velocity = Vector3.left;
        JumpPressed = false;
        isGround = false;
        isSpin = false;
        // rb.velocity = new Vector3(2,4,3);
        // rb.AddForce(Vector3.up * 50, ForceMode.Impulse); //modes: Accesleration, Force, Impulse, VelocityChange
    }

    void Update(){
        if(Input.GetButtonDown("Jump")){
            JumpPressed = true;
        }
        if(transform.position.y<0.28){
            isGround = true;
        }else{
            isGround = false;//This method not ideal. Probably use collision based? maybe another way would exist.
        }//Method is also very much model dependent. Not flexible

        if(Input.GetKey(KeyCode.Q)){
            isSpin = true;
        }else{
            isSpin = false;
        }
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        //Rigid body related fnc. is recommended to be placed in Fixed Update
        Debug.Log("update");
        // rb.AddForce(Vector3.right);

        Debug.Log(transform.position.y<0.28);

        if(JumpPressed&isGround){
            rb.AddForce(Vector3.up * 5, ForceMode.Impulse);
            JumpPressed = false;
        }
        if(isSpin){
            rb.AddTorque(Vector3.up * 3, ForceMode.Impulse);
        }
        

        Vector3 v = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical") );
        rb.AddForce(v, ForceMode.Impulse);
    

    }
}

0개의 댓글