- Home /
replace raycast with transform direction
i have working code here. i would like to replace the raycast with a more appropriate solution.
possible?
there will be 30-40 players all with this script on their player controller.
thanks in advance!
using UnityEngine;
//smoothly rotate a particle around a player
//at a set distance
//and magenetic to the target
public class rotatedemo : MonoBehaviour
{
GameObject pointB;
GameObject pointA;
public float interval = 1.0f;
void Start()
{
pointB = GameObject.Find("player");
pointA = GameObject.Find("target");
InvokeRepeating("Repeat", 0.01f, interval);
}
void Repeat()
{
//get a ray from point A in the direction of point B
//http://answers.unity3d.com/questions/32532/how-to-find-direction-between-two-points.html
Ray ray = new Ray(pointA.transform.position, (pointB.transform.position - pointA.transform.position).normalized);
//move the sphere 2 units away from the target
transform.position = ray.GetPoint(2);
//rotate the sphere to look point B
transform.LookAt(pointB.transform);
}
}
hierarchy.png
(4.4 kB)
Comment
repeating a raycast every second for 40 players is a bit intense. is this the best solution or is there a better way without the raycast? all 30-40 players will not have this active at all times - but im trying to achieve the most efficient solution.
Best Answer
Answer by KdRWaylander · Jul 20, 2015 at 07:50 AM
Hi,
I don't know why you shoot this ray, since you compute the direction juste before.
Try setting the transform.forward of your sphere equal to your AB direction and then add +2 to the forward direction.
Then, you use LookAt to replace the sphere.
Your answer
