Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by DolphinDudeOne · Jul 12, 2014 at 04:18 AM · javascriptphysicsrigidbodyaddforce

AddForce not working

I am currently using this script to make a monster chase the player:

 #pragma strict
 
 var target : Transform;
 var moveSpeed = 240;
 var rotationSpeed = 180;
 var myTransform : Transform;
 var myRigidbody : Rigidbody;
 
 function Awake()
 {
     myTransform = transform;
 }
  
 function Start()
 {
     target = GameObject.FindWithTag("Player").transform;
     
     myRigidbody = rigidbody;
 }
  
 function Update () {
 
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  
     myRigidbody.AddForce(myTransform.forward * moveSpeed * Time.deltaTime);
      
 }

For some reason though the monster doesn't actually move. It turns to look at the player but doesn't chase. Does anyone know the reason?

Comment
Add comment · Show 5
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 robertbu · Jul 12, 2014 at 04:20 AM 1
Share

Three things to check out:

  • Is your rigidbody marked as kinematic? If so it will not move.

  • Did you constrain movements in the rigidbody?

  • Is your rigidbody colliding with anything? Add an OnCollisionEnter() with a Debug.Log().

avatar image DolphinDudeOne · Jul 12, 2014 at 05:10 AM 0
Share

The rigidbody is not kinematic, no movements are constrained, and it isn't colliding with anything.

avatar image robertbu · Jul 12, 2014 at 05:36 AM 0
Share

I just tested your code, and it worked fine for me. Note that it requires that you have a game object in the scene (other this this one) tagged "Player" for this code to work. You may also want to turn gravity off. I suggest a few other things. Any variable that you do not want to change in the inspector be made private. 'moveSpeed' should really be 'moveForce' since the value is not directly related to the speed of the object. Also applying force repeatedly is better done in FixedUpdate() (and therefore will not need to be scaled by Time.deltaTime). So the fixing these problems would make the code:

 #pragma strict
  
 var moveForce = 24;
 var rotationSpeed = 180;
 
 private var target : Transform;
 private var myTransform : Transform;
 private var myRigidbody : Rigidbody;
  
 function Awake() {
     myTransform = transform;
     myRigidbody = rigidbody;
 }
  
 function Start() {
     target = GameObject.FindWithTag("Player").transform;
 }
  
 function Update () {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 }
 
 function FixedUpdate() {
     myRigidbody.AddForce(myTransform.forward * moveForce);
 }


But again, your original code worked for me.

avatar image DolphinDudeOne · Jul 12, 2014 at 07:40 AM 0
Share

It still doesn't want to move. It's probably something in the level like the floor or the roof then since when I used transform to move it, it worked fine (probably due to ignoring the physics of whatever it is colliding with now).

avatar image DolphinDudeOne · Jul 13, 2014 at 01:40 AM 0
Share

I've now got it working. I just added a box collider as well and he now follows the player without going through the walls. Thanks for the help!

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

2 People are following this question.

avatar image avatar image

Related Questions

Declaring myRigidbody 1 Answer

How do I add force To rigidbody based On Character's Walking direction? 0 Answers

How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers

Erratic Physics behaviour while testing in 2 computers 0 Answers

Make a player jump tile 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