- Home /
Move an object towards a point using raycast from Main Camera
I'm trying to make an RTS, so I need to be able to right-click to where I want my unit to go. So far, I've gotten it so that I can select a unit with a script in the main camera, and then click to teleport the unit to a point. I don't want the unit to teleport, but instead to move towards the point.
Here's my code for the movement:
void Update(){
if (Input.GetButtonDown ("Fire2") && target.tag == "Unit") {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
destination = hit.point;
target.rigidbody.MovePosition(destination);
}
}
}
It sounds like you want to Lerp from one point to another based on your current transform`s position and the position of your mouse relative to the world.
You should read this... ScreenToWorldPoint And this... Lerp
these will help you to solve your problem. :) If you would like the script revised to resolve your problem at hand, could you post the whole script please dude. It saves time guessing stuff, for stupid ol me at least. Thanks for reading Gruffy
I tried both, but neither worked. And thanks for offering to revise all of my code, but I'm kind of doing this to $$anonymous$$ch myself how to debug and whatnot.
Answer by MasonK · Jan 08, 2014 at 10:40 PM
Nevermind, I fixed it. First I converted the .MovePosition to .MoveTowards, but it still jumped around. It turns out that the code was only executing during the frame in which you right-clicked, so I moved it out of the if statement it was in and adjusted the code accordingly.
Here's the entire script, which is placed into the main camera. The way I set it up, you always need to have a target selected, so to prevent errors you need to put a gameObject with a rigidbody into "bTarg".
public class ClickTarget : MonoBehaviour {
private GameObject target; private Vector3 destination; private float distance; private Vector3 tTrans;
public GUIText targetDisplay; public float speed; public GameObject bTarg;
void Start () { targetDisplay.text = ""; distance = 0.0f; target = bTarg; }
void Update () { if(Input.GetButtonDown("Fire1")){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit)){ if(hit.collider != null){ if(hit.collider.tag == "Unit"){ target = hit.collider.gameObject; targetDisplay.text = "Unit: " + hit.collider.gameObject.name; destination = target.transform.position; target.rigidbody.freezeRotation = false; } if(hit.collider.tag == "Building"){ target = hit.collider.gameObject; targetDisplay.text = "Building: " + hit.collider.gameObject.name; } } } } }
void FixedUpdate(){ if (Input.GetButtonDown ("Fire2") && target.tag == "Unit" && GUIUtility.hotControl == 0) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray,out hit)){ destination = hit.point; } }
tTrans = target.transform.position;
distance = Vector3.Distance (tTrans, destination);
if(target.tag == "Unit"){
if (distance > .2f) {
target.transform.LookAt (destination);
target.transform.position = Vector3.MoveTowards (target.transform.position, destination, speed);
target.rigidbody.freezeRotation = true;
}
}
} }
tick your answer as correct to help others from community and please post your working script too
Answer by sath · Jan 08, 2014 at 09:14 PM
attach this script to a gameobject and it will follow mouse hit position
do the changes you need to make it work with your project
var speed = 1;
var targetRadius = 1;
private var target : Vector3;
function Start () {
// rigidbody.freezeRotation = true;
target = transform.position;
}
function FixedUpdate () {
target.y = transform.position.y;
if (Input.GetButtonDown ("Fire1")) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit)) {
target = hit.point;
}
}
if (Vector3.Distance(transform.position, target) > targetRadius) {
transform.LookAt(target);
transform.Translate(0,0,speed);
}
}
I need my code to be in my main camera so I don't have to put it into each an every unit, and so that I can select different units. I tried to modify this to fit into my camera, but the unit still just teleports around, which is what I was trying to fix in the first place.
Your answer
Follow this Question
Related Questions
OnMouseEnter/Exit problem 2 Answers
Positioning a GameObject so I't wont be inside another object 1 Answer
how to make my character go round loops? 1 Answer
error CS8025: Parsing error 1 Answer