ok so ive been working on this project to where the player has a “gun” which is just a cylinder right now with an empty object at the end called “spawner”, i have a prefab of the “ammo” which is a ball with a script attached that makes the ball stick to the object it hits, now what im trying to do is make it so when the ball hits and sticks (which works right now with rigidbodies) it pulls the player to the ball object. ive tried using this inside the ball script ex: player.position = Vector3.movetowards(player.position,this.transform.position, step); but it wont do anything. ive even tried just making a new script to move the player towards ball just using the “Vector3.movetowards” but for some reason my player isnt budging, nor is the ball for that matter (though i dont want it to… ive tried)… can anyone help me with this? racking my brain on it for hours and feel like im over thinking it. im just trying to simulate a grappling hook without a rope.
and yes i have looked and looked for a solution for a grappling gun, but everyone seems to be worried about the rope and never reveals how they got this to work.
ballstick.js
#pragma strict
var player : GameObject;
var pspeed : float;
function Start () {
}
function Update () {
var step = pspeed * Time.deltaTime;
player.transform.position = Vector3.MoveTowards(player.transform.position,this.transform.position, step);
//var dist = Vector3.Distance(player.position,this.transform.position);
//
//Debug.Log(dist);
}
function OnCollisionEnter(col : Collision){
var hj : HingeJoint;
hj = gameObject.AddComponent("HingeJoint");
hingeJoint.connectedBody = col.rigidbody;
transform.rigidbody.velocity = Vector3(0,0,0);
transform.rigidbody.useGravity = false;
transform.rigidbody.isKinematic = true;
//transform.parent = col.transform;
//transform.position = Vector3.zero;
//transform.rotation = Quaternion.identity;
}
and heres the script that shoots the ball
shootammo.js
#pragma strict
var bullet : Rigidbody;
var speed : float;
var player : Transform;
function Start () {
}
function Update () {
if(Input.GetButtonDown("Fire1")){
var clone : Rigidbody;
clone = Instantiate(bullet,transform.position,transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.up * speed);
}
}
EDIT UPDATE: ive tried spring joints between the ball and player character to no avail, but i did see movement using Vector3.lerp, but only when i fired the gun, and it instantly spawned onto the ball, stopping the balls trajectory and is definitely not what i want