- Home /
how to add force?
This is my code:
How can I add downward force?
public class PlayerMovement2 : MonoBehaviour
{
float playerHeight = 2f;
[SerializeField] Transform orientation;
[Header("Movement")]
[SerializeField] float moveSpeed = 6f;
[SerializeField] float airMultiplier = 0.4f;
float movementMultiplier = 10f;
[Header("Sprinting")]
[SerializeField] float walkSpeed = 4f;
[SerializeField] float sprintSpeed = 6f;
[SerializeField] float acceleration = 10f;
[Header("Jumping")]
public float jumpForce = 5f;
[Header("Keybinds")]
[SerializeField] KeyCode jumpKey = KeyCode.Space;
[SerializeField] KeyCode sprintKey = KeyCode.LeftShift;
[Header("Drag")]
[SerializeField] float groundDrag = 6f;
[SerializeField] float airDrag = 2f;
float horizontalMovement;
float verticalMovement;
[Header("Ground Detection")]
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask groundMask;
[SerializeField] float groundDistance = 0.2f;
public bool isGrounded { get; private set; }
Vector3 moveDirection;
Vector3 slopeMoveDirection;
Rigidbody rb;
RaycastHit slopeHit;
private bool OnSlope()
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
{
if (slopeHit.normal != Vector3.up)
{
return true;
}
else
{
return false;
}
}
return false;
}
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
MyInput();
ControlDrag();
ControlSpeed();
if (Input.GetKeyDown(jumpKey) && isGrounded)
{
Jump();
}
slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
}
void MyInput()
{
horizontalMovement = Input.GetAxisRaw("Horizontal");
verticalMovement = Input.GetAxisRaw("Vertical");
moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
}
void Jump()
{
if (isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.AddForce(transform.up * 12 * jumpForce, ForceMode.Impulse);
}
}
void ControlSpeed()
{
if (Input.GetKey(sprintKey) && isGrounded)
{
moveSpeed = Mathf.Lerp(moveSpeed, sprintSpeed, acceleration * Time.deltaTime);
}
else
{
moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
}
}
void ControlDrag()
{
if (isGrounded)
{
rb.drag = groundDrag;
}
else
{
rb.drag = airDrag;
}
}
private void FixedUpdate()
{
MovePlayer();
}
void MovePlayer()
{
if (isGrounded && !OnSlope())
{
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (isGrounded && OnSlope())
{
rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (!isGrounded)
{
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
}
}
}
Answer by rh_galaxy · May 07 at 08:19 AM
Add a ConstantForce component to the game object in the inspector.
ConstantForce customGravity;
void Start()
{
customGravity = GetComponent<ConstantForce>();
customGravity.force = new Vector3(0, 1, 0) * myScale; //set how you like, maybe
// depending on rb.mass or other things
customGravity.enabled = true;
}
void Update()
{
//the rigid body will add customGravity if enabled.
}
@rh_galaxy . MissingComponentException: There is no 'ConstantForce' attached to the "Player" game object, but a script is trying to access it. You probably need to add a ConstantForce to the game object "Player". Or your script needs to check if the component is attached before using it. UnityEngine.ConstantForce.set_force (UnityEngine.Vector3 value) (at :0) PlayerMovement2.Start () (at Assets/Scrips/RB/PlayerMovement2.cs:73) . This error cames up when I run it
Answer by stalwart5033 · May 09 at 11:43 PM
you can use something like this:
if (grounded) AddForce(Vector3.down gravityForce 100f);