- Home /
Game Object Tracker with Rotation of Observing game object
Hello all and thanks for reading.
I am attempting to do an objective tracker it will work like the Rocket League Ball Tracker when not camera locked.
I found this code on Unity Scripting/ Vector3.ClampMagnitude
and modified it to fit my needs. It works, as long as my vehicle is facing the Objective. If I rotate the vehicle it stops working. So I was thinking get that angle and do some stuff with that.
void Start () { radius = 5f; objective = GameObject.Find("CapturePoint"); centerPt = this.transform.position; centerPtLocal = this.transform.localPosition; }
// Update is called once per frame
void Update () {
//Face Objective
transform.LookAt(objective.transform);
//Get angle of Car with respect to Objective
Vector3 targetDir = objective.transform.position - transform.parent.transform.position;
float angle = Vector3.Angle(targetDir, transform.parent.transform.forward);
print(angle);
// Get the new position for the object.
Vector3 movement = objective.transform.position;
Vector3 newPos = transform.position + movement;
// Calculate the distance of the new position from the center point. Keep the direction
// the same but clamp the length to the specified radius.
Vector3 offset = newPos - centerPt;
Vector3 clamped = Vector3.ClampMagnitude(offset, radius);
Vector3 temp = new Vector3(clamped.x * Mathf.Sin(angle), 0f, clamped.z * Mathf.Cos(angle) );
transform.localPosition = centerPtLocal + temp;
Off the top of my head there are a few things you could be facing with the angle. Have you taken trig? I would also try using Vector3.SignedAngle() ins$$anonymous$$d of Angle.
There were two err that needed to be fixed 1) Vector.SignedAngle 2) convert degrees to Radians
cheers!
Answer by Jeigh07 · Feb 26, 2019 at 03:40 PM
This is the solution that has created the Rocket League tracker in 2 dimensions. There were two err that needed to be fixed 1) Vector.SignedAngle 2) convert degrees to Radians. Remeber that Mathf.Cos and ... Tan use Radians not Degrees!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/* Code takes this.Gobject or pointer which is alligned with Z axis and points it at target/objective
* it will rotate around parent Gobject
* pointer always resides at max radius
* changes position of pointer with respect to parent.forward and target
* only works in 3 axis.
* Created by Jeigh07
* inspired by Rocket League Pointer
*/
public class PointAtObjective : MonoBehaviour {
public GameObject objective;
public float radius;
private Vector3 centerPt;
private Vector3 centerPtLocal;
private Transform parentXform;
// Use this for initialization
void Start () {
radius = 2f;
objective = GameObject.Find("CapturePoint");//the objective
centerPt = this.transform.position;//the pointer which starts at center of center
centerPtLocal = this.transform.localPosition; //local position of center
parentXform = transform.parent.transform;
}
// Update is called once per frame
void Update () {
//Changes euler angles of pointer so that it may point to objective
transform.LookAt(objective.transform);
//Get the angle of car with respect to the objective in Radians
Vector3 targetDir = objective.transform.position - parentXform.position;
float angle = Vector3.SignedAngle(targetDir, transform.parent.transform.forward, Vector3.up);
angle *= Mathf.Deg2Rad;
// Get the new position for the object.
Vector3 movement = objective.transform.position;
Vector3 newPos = transform.position + movement;
// Calculate the distance of the new position from the center point. Keep the direction
// the same but clamp the length to the specified radius.
Vector3 offset = (newPos - centerPt) * 1000; //scaled to max out r
Vector3 clamped =new Vector3(
Mathf.Clamp(offset.x, -radius, radius),
0f,
Mathf.Clamp(offset.z, -radius, radius) ) ;
//Rotate the pointer object about the parent object with respect to the smallest angle in rads of parent and objective
Vector3 temp = new Vector3(clamped.x * Mathf.Sin(angle), 0f, clamped.z * Mathf.Cos(angle));
//pass value to local position of pointer
transform.localPosition = centerPtLocal + temp;
}
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
CCTV behavior rotation issue 2 Answers
Editing rotation of an Instantiated object 3 Answers
gameObject.transform.forward does not return expected value 0 Answers
Trigger diagonal rotation - Problem 2 Answers