• 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
Question by Socke97 · Apr 25, 2014 at 10:25 PM · c#movementrigidbodyaddforce

Player isn't moving (AddForce) (Hook)(c#)

I created a little script that shoots simple spheres as hook. When the hook hits something, it stop moving. And now the player should get pulled to the "hook", but nothing happens... Here is the Code, which is placed on the hook itself:

 using UnityEngine;
 using System.Collections;
 
 public class OnHook : MonoBehaviour {
 
     public GameObject player;
     public GameObject hook;
 
     public float speed = 20;
 
     public bool grabed = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
     
     }
     
 
     void OnTriggerEnter(Collider col){
 
         if(col.tag != "Player" && col.tag != "Hook")
         {
             grabed = true;
             rigidbody.velocity = Vector3.zero;
             player.transform.rigidbody.AddForce((player.transform.position - hook.transform.position).normalized * speed);
 
         }
 
     }
 }
 
Comment

People who like this

0 Show 1
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 Socke97 · Apr 26, 2014 at 10:00 AM 0
Share

doesn't anybody know why my player isn't moving?

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by RyanPaterson · Apr 26, 2014 at 11:19 AM

Hey! You could try this:

I've never really used rigidbodies, so I'm not sure if what i'm doing is unneccisary, but I tried it myself and got it to move to the 'hook'.

I couldn't get rigidbody to work from ontriggerenter, as it's only run the once on hit, so it probably would move it in the smallest amount ever -- Maybe, i'm a novice so I'm not too sure.

Either way, I used your 'grabed' bool (should be 'grabbed' ;) but i'll let you change that) and used that in update, so I know it will run the script while it's true.

Next I saw that you were adding force at (player - hook positions) when I think it should be (hook - player positions) otherwise you're pushing it away from it.

That's all I did really and tested it out, the player moves to the hook and then jitters about because the bool is still true.

For that I'd say, maybe have a collider on the hook that when the player hits it, it cancels out the 'grabbed' bool? possibly destroy the object as well. There's a few things you should add into the update function too like if(hook != null) etc.. to watch for bugs and stuff.

Anyway, hope this helped!

         public GameObject hook;
         public GameObject player;
         float speed = 100;
         
         public bool grabed = false;
 
         void Update () {
             
         if (grabed == true) {
 
             player.rigidbody.AddForce((hook.transform.position - player.transform.position).normalized * speed);
         }
         
         }
         
         
         void OnTriggerEnter(Collider col){
             
         print ("collided");
 
             if(col.tag != "Player" && col.tag != "Hook")
             {
 
                  print ("Hit - " + col.name);
                 grabed = true;
                         
             }
             
         }
Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Rigidbody falling very slow 1 Answer

Force to Velocity scaling? 2 Answers

Unity Physics On Input Issue? 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