2

Trying to get an object to fire towards and through a given position. Like a bullet firing towards the mouse location, I don't want it to stop on the mouse (which is what is happening now).

Below is what I have so far, is there a function like lerp that I could use?

var speed:float;
var startPoint:Vector3;
var startTime:float;
var clickedPosition:Vector3;


function Start()
{    
     startPoint = transform.position;
    startTime = Time.time;
    clickedPosition = Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
    clickedPosition = Camera.main.ScreenToWorldPoint(clickedPosition);  


}

function Update ()
{

 transform.position = Vector3.Lerp(startPoint, clickedPosition, (Time.time-startTime));

}
tylercomp
  • 871
  • 3
  • 13
  • 25

3 Answers3

2

I would suggest using a rigidbody component and then applying a force in the direction (while disabling gravitiy i guess).

The way you have it now you should probably get it to work with

var speed : float;

function Start()
{    
    speed = 1000.0f; // experiment with this, might be way too fast;

    ...
}

function Update()
{
    transform.position += (clickedPosition - startPoint) * speed * Time.deltaTime;
}

(clickedPosition - startPoint) should give you the direction in which you want to move the object, Time.deltaTime gives you the milliseconds since the last call of the Update function (you want this in here so that the object moves the same speed at different framerates) and speed is just a constant to adjust the velocity.

Tom
  • 627
  • 6
  • 11
  • thanks, seems close... bullet still only moves to the mouse position. I am hoping for it to move past. But no pun intended I think this puts me in the right direction – tylercomp Dec 08 '13 at 19:25
  • your problem might be that the point you click on isnt what you think it is. could it be that the object is moving away from you so that it looks as if it wouldnt move? is the object supposed to move in 2d or in 3d space? – Tom Dec 08 '13 at 19:50
  • It is supposed to be moving in 2d space – tylercomp Dec 08 '13 at 19:55
  • 1
    mmh ok then try to ignore one axis of clickedPosition like : clickedPosition.z = 0.0f; if z is the axis you're not using and your object lies on z = 0 – Tom Dec 08 '13 at 19:56
  • just to be clear, the clickedPosition isnt a position you click on. It probably is the position of your mouse when you start the game as it is in start() – Tom Dec 08 '13 at 20:02
  • locking the z position to 0 fixed it – tylercomp Dec 08 '13 at 20:23
1
    var speed:float;
var startPoint:Vector3;
var startTime:float;
var clickedPosition:Vector3;


function Start()
{    
     startPoint = transform.position;

}

function Update ()
{

    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        startTime = Time.time;
        clickedPosition = Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
        clickedPosition = Camera.main.ScreenToWorldPoint(clickedPosition);  
    }
     transform.position = Vector3.Lerp(startPoint, clickedPosition, (Time.time-startTime));

}
Santosh
  • 278
  • 2
  • 11
1

This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article on the same here

Move to Touch / Click Position - The Game Contriver

gameOne
  • 609
  • 1
  • 10
  • 22