- Home /
Quaternion LookRotation problems while moving up hill.
UPDATE: I have made a list of what I need help with. When I get one of these to work it will break the other. I need the things on this list to work together at the same time.
I need the turret to stay attached to the tank properly while going up / down hill.
I need the turret and gun to follow the camera's raycast and line up accurately while going up / down hill.
I need to be able to control the rotation speed of the turret.
Hello, I am having a few problems with a LookRotation that follows a camera's raycast. I am controlling a tank's turret and gun with a Raycast from the center of a camera, so the turret and gun look where the camera looks. When I am on flat ground the turret pivot and gun pivot look at the camera's raycast correctly, but the first problem I encountered was driving up a angled surface. The turret and gun axis would break. I fixed this issue by adding a line of code which 0s out the euler angles for the rotation on the x and z axis (As I only care about Y on the turret). After doing that it messed up the accuracy of the Quaternion.LookRotation. Now when I drive up a hill the rotation of the turret pivot and gun pivot is not lining up which is causing the accuracy to be off. I have added my code and some screen shots of my problems. I need help to stop the turret from flipping off its axis while keeping the accuracy of the lookrotation tracking the camera's movement when going up hill. Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRaycast : MonoBehaviour
{
public Transform TurretPivot;
public Transform GunPivot;
public float TurretRotationSpeed;
public float GunRotationSpeed;
private RaycastHit Hit;
void Start ()
{
TurretPivot = TurretPivot.GetComponent<Transform>();
GunPivot = GunPivot.GetComponent<Transform>();
}
void Update ()
{
TurretFollowCameraRay();
GunFollowCameraRay();
}
void TurretFollowCameraRay()
{
if (Physics.Raycast(transform.position, transform.forward, out Hit))
{
Vector3 CameraRayDirection = Hit.point - TurretPivot.transform.position;
Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection);
TurretPivot.transform.rotation = Quaternion.RotateTowards(TurretPivot.transform.rotation, RotateToPosition, TurretRotationSpeed * Time.deltaTime);
// This line of code fixes the turret's pivot axis, but breaks the accuracy of the LookRotation.
TurretPivot.localEulerAngles = new Vector3(0,TurretPivot.localEulerAngles.y, 0);
}
}
void GunFollowCameraRay()
{
if (Physics.Raycast(transform.position, transform.forward, out Hit))
{
Vector3 CameraRayDirection = Hit.point - GunPivot.transform.position;
Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection);
GunPivot.transform.rotation = Quaternion.RotateTowards(GunPivot.transform.rotation, RotateToPosition, GunRotationSpeed * Time.deltaTime);
// This line of code fixes the gun's pivot axis, but breaks the accuracy of the LookRotation.
GunPivot.localEulerAngles = new Vector3(GunPivot.localEulerAngles.x, 0, 0);
}
}
}
Answer by jackishere · Mar 18, 2017 at 01:44 PM
Your code really rely on raycast so there's chance that your raycast might not hit anything when aiming up,
you can try add invisible collider on the roof or pass a hit point if the raycast did not hit anything
I am having these problems even when the raycast is hitting objects.
Hey is there's anyway i can get the $$anonymous$$imal sample of the project so i can help you better?
Answer by IgorAherne · Mar 18, 2017 at 01:15 AM
TurretPivot.transform.rotation
redundand code. You already declared TurretPivot as Transform. just use TurretPivot.rotation
instead. Otherwise it's like this.gameObject.transform.transform.rotation
Now to the problem:
just use:
//specify rotation, where second argument is what should be the "upwards dir of the object"
Quaternion RotateToPosition = Quaternion.LookRotation(CameraRayDirection, TurretPivot.up);
if you are using turret.FBX, try .right
instead
for Gun use GunPivot.right
, because it rotates around its local right axis
And then dispose of the y-angles code and the x-angles code you were using.
IgnorAherne by default if you do not specify the 2nd argument in LookRotation it is classed as Vector3.up so unless he got the wrong axis that is ok coding.
The fact it works on ground but not on the angle is confusing to me as it seems the code is using is correct to zero out both axis.
yes, but it's Vector3.up by default, which is different to transform.up (the local up of an object). For example, if we treat Earth's north as the Vector3.up, my up direction in U$$anonymous$$ is different to the Vector3.up, and is different to the up direction to someone on the opposite side of Earth.
I tried the code you suggested and it did not fix the problem. The turret still has the problem of not staying attached to the tank. I need help with three things.
I need the turret to stay properly attached to the tank when going up/down hill.
I need the turret and gun to accurately follow the camera's raycast and line up properly.
I need to be able to control the rotation speed of the gun and turret.
I need to have those three things work together. I have only been able to get them to work one at a time and then they break each other.