Could someone help me with my script? "How to translate object to a Vector3 of another object using raycast"
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MoveToObject : MonoBehaviour {
public bool isMoving = false;
public float lockPos = 0;
public Vector3 CP;
public Vector3 hitGameObject;
public string planet;
private GameObject Objective;
private Vector3 YourDestination;
void Start ()
{
}
void FixedUpdate()
{
transform.rotation = Quaternion.Euler(lockPos, lockPos, lockPos);
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
CP = transform.position;
isMoving = true;
if (hit.transform.tag == "Planet")
{
hitGameObject = (hit.point);
Vector3 YourDestination = new Vector3(hitGameObject.x, hitGameObject.y, -4);
//hit.collider.gameObject.name = planet;
//Objective = GameObject.Find(planet);
//Objective.transform.position = hitGameObject;
//YourDestination = new Vector3 (hitGameObject.x, hitGameObject.y, -4);
}
}
if (isMoving)
{
//transform.Translate ((CP - YourDestination) * Time.deltaTime);
transform.Translate ((CP - YourDestination) * Time.deltaTime);
}
}
}
}
We have a spaceship. If i press on the planet on the certain distance from me - spaceship should go to it's coordinates X/Y but the coordinate Z should still be -4. It just doesn't move but it shows me the coordinates of me raycast hit.point...
Answer by AtGfx · May 08, 2017 at 11:29 AM
Hello @salevan3 !
Here is your working script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToObject : MonoBehaviour
{
public bool isMoving = false;
public float lockPos = 0;
public Vector3 CP;
public Vector3 hitGameObject;
public string planet;
private GameObject Objective;
private Vector3 YourDestination;
void Start()
{
}
void FixedUpdate()
{
transform.rotation = Quaternion.Euler(lockPos, lockPos, lockPos);
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
CP = transform.position;
if (hit.transform.tag == "Planet")
{
isMoving = true;
hitGameObject = (hit.point);
YourDestination = new Vector3(hitGameObject.x, hitGameObject.y, -4);
}
}
}
if (isMoving)
{
transform.Translate((YourDestination - CP) * Time.deltaTime);
if (Vector3.Distance(transform.position, YourDestination) < 0.1)
{
isMoving = false;
}
}
}
}
A couple of mistake were made :
put your isMoving block outside the Input.GetMouseButtonDown block : it allows to move your object after you have clicked a planet
assing isMoving = true after you check that the tag is planet otherwise you will move when you click something else
when you calculate a displacement it is destPos - initialPos (so YourDestination - CP is correct, CP - YourDestination is not)
add a check to determine if your object is close enough to your planet to stop it moving
You redefine a YourDestination vector when you assign it... so the value is lost at next update
Your answer
![](https://koobas.hobune.stream/wayback/20220612121507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to check if an object is betwen enemy and player 1 Answer
Ballistic vector technique 0 Answers
transform position don't work with scaled parent 0 Answers
Raycast under crosshair 0 Answers