- Home /
Need help moving player object towards an instantiated ball
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
This is not an answer, this is just his code reformatted with the empty Start() functions removed, I'm going to sleep(its late here) but I hope this saves whoever does answer this a few $$anonymous$$utes time :
#pragma strict
var player : GameObject;
var pspeed : float;
function Update () {
var step = pspeed * Time.deltaTime;
player.transform.position = Vector3.$$anonymous$$oveTowards(player.transform.position,this.transform.position, step);
}
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.is$$anonymous$$inematic = true;
}
#pragma strict
var bullet : Rigidbody;
var speed : float;
var player : Transform;
function Update () {
if(Input.GetButtonDown("Fire1")){
var clone : Rigidbody;
clone = Instantiate(bullet,transform.position,transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.up * speed);
}
}
thank you for that, sorry i didnt even think about cutting those out, ive been stressing myself out over this.
Answer by jdog98 · Nov 01, 2014 at 02:10 AM
Try transform.LookAt(target); it turns the player to look at target . If you put a force forward on the player, then it should move towards target
ok ive found a nice way to get my player character to go to a point, using Vector3.slerp, and messing with the player gravity, but how do i reference an instantiated object, this is the missing piece to my puzzle now, because they keep co$$anonymous$$g up as "Ball ammo(Clone)" so there is no unique identifier to the object for me to use it as a target that way, im on to the solution now, if someone can help me figure that out, and thank you so much for answering to help. many cheers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170432im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Set Rigidbody.velocity for an instantiated object 1 Answer
Help On Checking OverLaping in instantiating 0 Answers
Instantiate object to move with other gameObject 0 Answers
Need auto aim 0 Answers