Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges