Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jul 20, 2016 at 06:39 PM by matthrewp.
avatar image
0
Question by matthrewp · Jul 19, 2016 at 07:59 PM · scripting problemrigidbody.addforce

Player movement not working

I have this script to move the player (and their gun) and I can't figure out why it's not working. The debug.log("blah") things show me that the if statements are activating but the player doesn't move at all. Here is the script:

 using UnityEngine;

 public class PlayerController : MonoBehaviour {

     public GameObject pBody;
     public GameObject pGunOrigin;
     public static float speed;
     Vector3 gOffset;
     private bool right;
     private bool left;
     private bool forward;
     private bool backward;
     public Rigidbody rbody;
     void Start () {
         gOffset = pGunOrigin.transform.position - transform.position;
         rbody = GetComponent<Rigidbody>();
     }
     void FixedUpdate () {
         right = Input.GetKey(KeyCode.RightArrow);
         left = Input.GetKey(KeyCode.LeftArrow);
         forward = Input.GetKey(KeyCode.UpArrow);
         backward = Input.GetKey(KeyCode.DownArrow);
         if (right == true && left == false)
         {
             Debug.Log("Right");
             rbody.AddForce(new Vector3(speed, 0, 0), ForceMode.VelocityChange);
         }
         if (left == true && right == false)
         {
             Debug.Log("Left");
             rbody.AddForce(new Vector3(-speed, 0, 0), ForceMode.VelocityChange);
         }
         if (forward == true && backward == false)
         {
             Debug.Log("Forward");
             rbody.AddForce(new Vector3(0, 0, speed), ForceMode.VelocityChange);
         }
         if (backward == true && forward == false)
         {
             Debug.Log("Backward");
             rbody.AddForce(new Vector3(0, 0, -speed), ForceMode.VelocityChange);
         }
         pGunOrigin.transform.position = pBody.transform.position + gOffset;
     }
 }
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

1 Reply

  • Sort: 
avatar image
0

Answer by mnmwert · Jul 19, 2016 at 09:34 PM

your code does not need to be so complicated. Instead of having

if(bool = true)

you can just say

if(this button is pressed)

here is an example

 if(Input.getKey(KeyCode.UpArrow))
 {
    GetComponent<Rigidbody>().velocity = new Vector3(0,0,speed);
 }

you may need to use a different way to add force to the player but hopefully i made my point.

I believe in your code the problem lies within the statements

 rbody.AddForce(new Vector3(0, 0, -speed), ForceMode.VelocityChange);

try changing ForceMode.VelocityChange to ForceMode.Impulse. (I hear it is better to use that)

I hope this helps

Comment
Add comment · Show 1 · Share
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
avatar image matthrewp · Jul 19, 2016 at 10:43 PM 0
Share

Thanks for trying, but it didn't work.

Follow this Question

Answers Answers and Comments

58 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

Related Questions

Correctly move rigidbody at constant speed 1 Answer

Rigidbody2D AddForce not working 0 Answers

making a mushroom trampoline , bouncing the gameobject relative to mushroom's rotation 1 Answer

How to detect an object which be in FOV of certain camera ? 1 Answer

EditorCurveBinding.FloatCurve propertyName for scale 0 Answers


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