Hi everyone,
I’m trying to get my instantiated object to face the direction of the previously instantiated object. I can’t get it to work however as the y rotation keeps coming out as 0. Any help would be much appreciated!
The Target is the last created object’s position, Origin is the position of the current one being created.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseDraw : MonoBehaviour
{
public GameObject myPrefab;
public Camera mainCamera;
public Vector3 Target;
public Vector3 Origin;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit raycastHit))
{
}
Origin = raycastHit.point;
Vector3 dir = Target - Origin;
Quaternion rotation = Quaternion.Euler(0, dir.y, 0);
Instantiate(myPrefab, new Vector3(raycastHit.point.x, 0, raycastHit.point.z), rotation);
Target = raycastHit.point;
}
}
}