- Home /
object sucked towards object
Im trying to make it so that when i right click i shoot at objects and when i left click the object im aiming at gets sucked towards my players weapon and stays there and then im able to click and shoot the object. This is kinda a weird question so im having a hard time finding anything to help me.
Answer by spinaljack · Jul 17, 2010 at 08:48 PM
So basically you want to make the gravity gun from half life 2.
What you need to do is use AddForce() on the rigid body you're pointing at (use a raycast for this) to move it towards your player(use look rotation to find the directing towards something). When it's within reach you can start manipulating its position directly to stay directly in front of you. To shoot it off just use AddForce() again but in the opposite direction and with a greater force.
If you wanted the attracting force to be distance relative you you can use this basic formula:
force = power/distance^2
With a maximum distance cut off point. The force an object experiences drops off rapidly the further away it is, like with gravity.
EDIT:
To keep something in front of the player with scripts you need to update the position of the object to match the player's position and then you add the forward vector of the player to keep it some distance in front of the player. For this you can use transform.TransformDirection()
e.g.
var player : GameObject; var distance : float = 1.0;
function update(){ transform.position = player.transform.position + player.transform.TransformDirection(Vector3.forward)*distance; }
Yea i guess its pretty much a gravity gun. Its a vacuum. :P. I will give this a shot thanks for the help.
ok so i have it sucking towards me. But how do i get it to stop at a point? Right now that is where im stuck.
You need to check its distance from the player and if it's within a meter you should either parent it to the player so that moving the player moves the object or you should add a fixed joint or you should manipulate the position of the object with scripts. Any number of options would work.
Yea i want to do it with scripts, but im really new and dont really know what to do. I dont want you to write my script for me but could you kinda tell me what i have to do to start it. b/c once i get it going i can ussualy figure out what to do...
Answer by Spatiulus · Jun 18, 2012 at 07:11 AM
Hello! I have a resembling question, but I am a little stupid in codes...( Could you, please, explain how to use Force, Raycast and Look Rotation at greater length?
I created a script like this:
var target : Transform;
function Update () {
var relativePos = target.position - transform.position;
var rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
}
function FixedUpdate () {
rigidbody.AddForce (Vector3.forward * 10);
}
But that doesn't seem to work correct... And I don't understand how to use Raycast with those two scripts, should this script be attached to the rigid body or to the camera?
Please forgive me those primitive questions. I would be very thankful if you could help! I can't find an answer for that a very long time...
Yours respectfully, Stephan Rotar
Answer by Rotarus · Jun 25, 2012 at 12:37 AM
I found a way to do that, using Vector3.Lerp and my player position. Here is the CODE:
var target : GameObject;
target = GameObject.Find ("Player");
var AttractOn = 0;
var smooth = 1.0;
function OnMouseOver() {
if (AttractOn == 1){
gameObject.tag = "AttractToPlayer";
}
}
function Update () {
if (Input.GetMouseButtonDown(1) && AttractOn == 0){
AttractOn = 1;
}
if (Input.GetMouseButtonUp(1) && AttractOn == 1){
AttractOn = 0;
}
if (gameObject.tag == "AttractToPlayer"){
transform.position = Vector3.Lerp(transform.position,target.transform.position,Time.deltaTime*smooth);
Your answer
Follow this Question
Related Questions
Weapon switching 1 Answer
make Enemy shoot at player 2 Answers
Making a shotgun? 1 Answer
Using RayCast to do Continuous Shooting? 1 Answer
Making a gun shoot WELL? 1 Answer