Question by
BluCodeUE · May 10, 2017 at 02:12 PM ·
rigidbodyaddforcenot workingc++
RigidBogy.AddForce does nothing.
I am trying to make a first person character controller, but my main movement AddForce call does nothing. I have already tried with very large numbers for the vector to move, with no effect. My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonController : MonoBehaviour {
public Camera cam;
public float forwardSpeed = 8.0f;
public float backwardSpeed = 4.0f;
public float strafeSpeed = 4.0f;
public float runMultiplier = 2.0f;
public float jumpForce = 30f;
public bool airControl = false;
public float mouseSensitivity = 5.0f;
public KeyCode RunKey = KeyCode.LeftShift;
public KeyCode ESCKey = KeyCode.Escape;
public KeyCode JumpKey = KeyCode.Space;
private Rigidbody rb;
private CapsuleCollider cc;
private Quaternion originalRotation;
private Quaternion camOriginalRotation;
private float rx = 0.0f;
private float ry = 0.0f;
private bool paused = false;
private float targetSpeed = 0.0f;
private Vector3 targetMove;
private bool isGrounded = false;
private bool previouslyGrounded = false;
private Vector3 groundNormal = Vector3.up;
private bool jumping = false;
private bool jump = false;
void Start () {
rb = GetComponent<Rigidbody> ();
cc = GetComponent<CapsuleCollider> ();
rb.freezeRotation = true;
originalRotation = transform.localRotation;
camOriginalRotation = cam.transform.localRotation;
}
void Update () {
RotateCamera ();
updatePaused ();
getInput ();
}
void FixedUpdate () {
Move ();
}
void RotateCamera () {
if (!paused) {
rx += Input.GetAxis ("Mouse X") * mouseSensitivity;
ry = Mathf.Clamp(ry + Input.GetAxis ("Mouse Y") * mouseSensitivity, -90f, 90f);
Quaternion xq = Quaternion.AngleAxis (rx, Vector3.up);
Quaternion yq = Quaternion.AngleAxis (ry, Vector3.left);
transform.localRotation = originalRotation * xq;
cam.transform.localRotation = camOriginalRotation * yq;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
}
void Move () {
GroundCheck();
if (rb.velocity.sqrMagnitude < (targetSpeed * targetSpeed) && targetSpeed > 0) {
rb.AddForce (targetMove, ForceMode.Impulse); //This is the problem
Debug.Log (targetMove);
}
if (isGrounded) {
rb.drag = 0f;
if (jump) {
rb.drag = 0f;
rb.velocity = new Vector3 (rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce (new Vector3 (0f, jumpForce, 0f), ForceMode.Impulse); //But this one works
jumping = true;
}
if (!jumping && targetSpeed > 0 && rb.velocity.magnitude < 1f) {
rb.Sleep ();
}
} else {
rb.drag = 0f;
}
jump = false;
}
void getInput () {
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
targetSpeed = 0.0f;
if (Mathf.Abs (h) > 0) {
targetSpeed = strafeSpeed;
}
if (v < 0 && (Mathf.Abs (h) != 0 || backwardSpeed > strafeSpeed)) { //Always use the faster speed
targetSpeed = backwardSpeed;
}
if (v > 0 && (Mathf.Abs (h) != 0 || forwardSpeed > strafeSpeed)) {
targetSpeed = forwardSpeed;
}
if ((Mathf.Abs(h) > 0 || Mathf.Abs(v) > 0) && (airControl || isGrounded)) {
targetMove = cam.transform.forward*v + cam.transform.right*h;
targetMove = Vector3.ProjectOnPlane(targetMove, groundNormal).normalized;
Debug.Log (targetMove);
targetMove.x = targetMove.x*targetSpeed;
targetMove.z = targetMove.z*targetSpeed;
targetMove.y = targetMove.y*targetSpeed;
}
if (Input.GetKeyUp (JumpKey) && !jump) {
jump = true;
}
}
void updatePaused () {
if (!paused && Input.GetKeyDown (ESCKey)) {
paused = true;
Time.timeScale = 0.0f;
Time.fixedDeltaTime = 0.0f;
} else if (paused && Input.GetKeyDown (ESCKey)) {
paused = false;
Time.timeScale = 1.0f;
Time.fixedDeltaTime = 0.02f;
}
}
void GroundCheck()
{
previouslyGrounded = isGrounded;
RaycastHit hitInfo;
if (Physics.SphereCast(transform.position, cc.radius, Vector3.down, out hitInfo,
((cc.height/2f) - cc.radius) + 0.01f, Physics.AllLayers, QueryTriggerInteraction.Ignore))
{
isGrounded = true;
groundNormal = hitInfo.normal;
}
else
{
isGrounded = false;
groundNormal = Vector3.up;
}
if (!previouslyGrounded && isGrounded && jumping)
{
jumping = false;
}
}
}
I have another AddForce for jumping, which works, so I have no idea what is causing the problem. Any help is appreciated.
Comment
Your answer
Follow this Question
Related Questions
How to AddForce to a Rigidbody from Game Objects Spawned from an Array, One Prefab at a Time? 2 Answers
Player going through walls 1 Answer
Spawning a Ball going forward every 5 seconds error, help! 2 Answers
Why does my rigidbody move it's transform while rotating? 1 Answer
How to change the direction of a rigidbody without adding to its speed? 1 Answer