Use Quaternion.lookat to rotate On One Axis (for turret.)
Hi gang, here is what I'm working on for my Automatic Gun-turret control system. I can select a random target with tag and when it gets out of range it try to find a new target Cool, 'my problem now is when i try to use Quaternion.lookat I cannot seem to get it to lock on Axis
using UnityEngine;
using System.Collections;
public class TurretTargeting : MonoBehaviour {
public GameObject Target;
public GameObject TurretBarrels;
public GameObject TurretHead;
public GameObject[] Targets;
public bool OurTeam;
public float distance;
public int index;
public void LateUpdate(){
distance = Vector3.Distance(Target.transform.position, transform.position);
// If the turret is ours........
if(OurTeam == true){
if (Target == null){
Targets = GameObject.FindGameObjectsWithTag("TeamTwo");
index = Random.Range (0, Targets.Length);
Target = Targets[index];
}
}
// If the turret is ours........
// If the turret is NOT ours........
if(OurTeam == false){
if (Target == null){
Targets = GameObject.FindGameObjectsWithTag("TeamOne");
index = Random.Range (0, Targets.Length);
Target = Targets[index];
}
}
// If the turret is NOT ours........
// Universal Targeting Range.
if(distance > 100){
Target = null;
}
// TurretBarrels ROATATE OON VERTICAL AXIS ONLY.
// TurretHead Rotate horizontal axis only.
}
}
Answer by Creeper_Math · Jan 17, 2017 at 07:35 PM
Sidenote...
Your positioning of the Distance formula is weird.. This is what your saying "Find the Distance, then select a random enemy, then test to see if the distance is more..." Don't know if you were trying to do that or not
Back to your Question
Here's a bit of code that would work (With Explanation)
Would be put where you want to rotate the turret
if (Target != null) {
// Saves it's current rotation, turns to target, records rotation, and reverts to original rotation immediately. All in one update so it should not show it looking at the target and looking back
Quaternion CurrentRotation = transform.rotation;
transform.LookAt(Target.transform.position);
Quaternion GoalRotation = transform.rotation;
transform.rotation = CurrentRotation;
// Makes Goal Rotation only save it's y value and set the current rotation's y value to the current rotation
// This can be Changed... The way to do that is to put in "GoalRotation" for all the axis that you want to change, and "CurrentRotation" for those that you don't want changed
GoalRotation = new Quaternion (CurrentRotation.x, GoalRotation.y, CurrentRotation.z, CurrentRotation.w);
float Speed = 1.0f;
transform.rotation = Quaternion.RotateTowards (CurrentRotation, GoalRotation, Speed);
}
if (Target != null){
TurretBarrels.transform.rotation = Quaternion.Slerp(transform.rotation, (Quaternion.LookRotation(Target.transform.position - this.transform.position)), Time.deltaTime * TurnSpeed);
}
if (Target != null){
TurretHead.transform.rotation = Quaternion.Slerp(transform.rotation, (Quaternion.LookRotation(Target.transform.position - this.transform.position)), Time.deltaTime * TurnSpeed);
}
this works, (what I'm currently using, but its on every AXIS and looks super strange)
I'm basically trying to do this but lock Gunbarrel to vertical rotation and lock Base to horizontal
GoalRotation = new Quaternion (CurrentRotation.x, GoalRotation.y, CurrentRotation.z, CurrentRotation.w);
This is where it takes the rotation of the object, and only the ones that you want to be changed remain to be the goal rotation, while the others switch back to the original rotation.. If you have different objects, then you would have to have a separate copy of the entire script in my answer for each one
Your answer
Follow this Question
Related Questions
Character rotation on ledge hanging 0 Answers
Problem with Quaternion and Euler Angles 0 Answers
Delayed LookAt, but only on one axis 1 Answer
How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject 0 Answers
Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers