Question by
salevan3 · May 09, 2017 at 05:50 AM ·
raycastvector3transform.translategameobject.findhit.point
How to translate Player's position to another GameObject's position using raycast. Help me please with my 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;
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...
Comment
Your answer
Follow this Question
Related Questions
Why isnt my raycast hitting the floor? 0 Answers
How to offset a raycast? 0 Answers
Right Click and Drag Formation Line RTS 0 Answers
Wall sliding with characterController against an edge 0 Answers