- Home /
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);
}
}
}
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;
}
}
Your answer
Follow this Question
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
WheelColliders Bug Fix 0 Answers