- Home /
Player isn't affected by AddForce(), Lerp() etc.
Hey there, I am trying to make a hook since a loooooong while. Now I am nearly done, but the player isn't moving. So I tried to use my script on another object (a simple sphere) and it worked...
The next thing I did, was disabling all scripts of my player (not the hook script), but the player still didn't move. My hook-scipts: On the player I have got a really simple script, which shoots a sphere as a hook. On the hook itself I have got a script that stops the hook if it's touching something and then move the player to the position of the hook.
Scipt on the hook: using UnityEngine; using System.Collections;
public class OnHook : MonoBehaviour {
public GameObject player;
public GameObject hook;
GameObject other;
public float speed = 20;
public bool grabbed = false;
// Use this for initialization
void Start () {
other = GameObject.FindWithTag ("Enemy");
}
// Update is called once per frame
void Update () {
if(grabbed)
{
// other.rigidbody.AddForce((transform.position - other.transform.position).normalized * speed); // This was my try using addForce
player.transform.position = Vector3.Lerp(player.transform.position, hook.transform.position, Time.deltaTime * speed);
}
}
void OnTriggerEnter(Collider col){
if(col.tag != "Player" && col.tag != "Hook")
{
grabbed = true;
rigidbody.velocity = Vector3.zero;
}
}
}
Thank you for your help!
Doesn't anybody know, why my player isn't moving or maybe just an idea?
Answer by Bunny83 · Apr 27, 2014 at 10:16 AM
Well, we don't know how your scene is setup, if you have your collision matrix setup correctly ...
Here's a small checklist:
The OnHook script is attached to your hook object
The hook object has a rigidbody attached.
The hook has a primitive collider (sphere, box, ...) or a convex MeshCollider
The collider is set to isTrigger.
The "hook" variable is linked to the hook object itself (which is actually pointless since you could just use "transform" as this script is on the hook object)
The "player" variable is linked to the correct player object.
Add a Debug.Log in line 34 to see if the trigger is activated and grabbed is set to true
You might also want to set rigidbody.isKinematic to true when you stop the hook so it "really" can't move anymore.
Make sure you don't have any other scripts which set the position of the player and interfere with your OnHook script.
Make sure that the layers the hook is on and the geometry you want to hit are on layers that can collider with each other.
Thank you!!!!!!! Finally i found the problem. You are right I didn't linked the existing player. I linked the prefab. THAN$$anonymous$$ YOU!