Hi I am using a rigidbody component attached to my game Object and I don't want to disable gravity but the game object keeps on falling. Moreover the enemy capsule just falls below the ground in my game. Im a little new to unity. The game I am making is with the help of the (Learning c# by developing games unity 2021) this is the 6th version. The script for the player behaviour is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehavoiur : MonoBehaviour
{
public float MoveSpeed = 10f;
public float RotateSpeed = 75f;
public int hp = 0;
/// <summary>
/// stores a rigidbody component for a gameobject
/// </summary>
private Rigidbody _rb;
// Start is called before the first frame update
void Start()
{
_rb = GetComponent<Rigidbody>();
_rb.useGravity = true;
}
private float _hInput1;
private float _vInput1;
// Update is called once per frame
void Update()
{
/*MoveForward(_hInput1);
RotateUpWards(_vInput1);
*/
}
/*public void MoveForward(float _vInput)
{
_vInput = Input.GetAxis("Vertical") * MoveSpeed;
this.transform.Translate()
}
public void RotateUpWards(float _hInput)
{
_hInput = Input.GetAxis("Horizontal") * RotateSpeed;
this.transform.Translate(Vector3.forward * _hInput * Time.deltaTime);
}
*/
void FixedUpdate()
{
SetVerticalDirection();
SetHorizontalDirection();
//makes the object move by horizontal input in the up direction
Vector3 rotation = Vector3.up * _hInput1;
/*this is the rotation of the game object
* moving up and then going at a fixed frame rate
*/
Quaternion angleRotation = Quaternion.Euler(rotation * Time.fixedDeltaTime);
//this takes vertical input and addes the position and makes it move forward
//the vertical input is the vertical axis whihc moves at a defined rate tht we can define
_rb.MovePosition(this.transform.position
this.transform.forward * _vInput1 * Time.fixedDeltaTime);
/*the move rotation moves using the rigibody rotation times the
* horizontal input times and moves up and rotates at the rotate speed rate
*/
_rb.MoveRotation(_rb.rotation * angleRotation);
}
void SetVerticalDirection()
{
_vInput1 = Input.GetAxis("Vertical") * MoveSpeed;
}
void SetHorizontalDirection()
{
_hInput1 = Input.GetAxis("Horizontal") * RotateSpeed;
}
/*Horizontal: ad
* Vertical: ws
*/
}
CodePudding user response:
In the Inspector, you can set gravity scale to 0. Or set the mass to 0. If none works, go to the Rigdid Body and use the Constraints and freeze the y position. this will stop it from falling
CodePudding user response:
I am also currently studying this book. You should check the third box in the RigidBody Freeze Rotation.