0
\$\begingroup\$

enter image description here

The object have forwardForce.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb; 
    public float forwardForce=800f;
    void Start()
    {
        
    }

    void FixedUpdate()
    {
        rb.AddForce(0,0,forwardForce * Time.deltaTime);
        if(Input.GetKey("d")){
            rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
        }else if(Input.GetKey("a")){
            rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);   
        }
    }
}

Now, the issue is I want to stop that object while falling down(not on that ground anymore).The camera will keep running but, I just want to stop that object. Which means if the is not on ground anymore than velocity=0

enter image description here

\$\endgroup\$
7
  • \$\begingroup\$ you want to reset the level, teleport the player back on the track or just stop the camera from following? can your player jump (or is a simple ground check enough to determinate if he fell off). \$\endgroup\$
    – Zibelas
    Feb 9 at 9:12
  • \$\begingroup\$ @Zibelas No! I don't wanna reset. Look while the object is falling the z axis increasing which means the object is traveling through z axis. I want not to change the z axis the object is falling. \$\endgroup\$
    – user147354
    Feb 9 at 9:13
  • \$\begingroup\$ You can lock the axis to prevent rotation \$\endgroup\$
    – Zibelas
    Feb 9 at 9:23
  • \$\begingroup\$ @Zibelas Sorry! I am new to game development. I couldn't get you. How to lock the axis? \$\endgroup\$
    – user147354
    Feb 9 at 9:23
  • \$\begingroup\$ Take a look at the constraint properties of the rigidbody of your object \$\endgroup\$
    – Zibelas
    Feb 9 at 9:24
0
\$\begingroup\$
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb; 
    public float forwardForce=1000f;
    

    void FixedUpdate()
    {
        rb.AddForce(0,0,forwardForce * Time.deltaTime);
        if(Input.GetKey("d")){
            rb.AddForce(30 * Time.deltaTime,0,0, ForceMode.VelocityChange);
        }else if(Input.GetKey("a")){
            rb.AddForce(-30 * Time.deltaTime,0,0, ForceMode.VelocityChange);   
        }
        if(Physics.Raycast(transform.position,Vector3.down,0.5f)==false){
            rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
        }else{
            
        }
    }
}

I tried by detecting the player is onGrounded or not. Then, I set velocity = 0 if player isn't grounded

Thanks Zibelas

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy