Hey,
I’ve created some simple code for making a "Chest"
rotate correctly towards a target.
The script works fine unless "Chest"
is a bone if it is, then the rotation seems to be off. The code definitely works because I tried it on cubes, it only occurs on bones
code.
This first calculates the direction of where i need to look, then subtracts that by the relative rotation of GunTip
to TransformLook
ie this code will rotate TransformLook
so that GunTip
(its grand child) points at "lookat"
:
Vector3 direction = Lookat - GunTip.position;
Quaternion targetRotation = Quaternion.LookRotation(direction) * Quaternion.Inverse(Quaternion.Inverse(TransformLook.rotation) * GunTip.rotation);
TransformLook.rotation = Quaternion.Lerp(TransformLook.rotation, targetRotation, 0.8f);
Rotation issue video (ignore AAA graphics, its supposed to be like that)
Screenshot of hierarchy:
Important parts of script:
(I’ve deleted useless bits but yes, everything is referenced properly)
using Cinemachine;
using System.Collections;
using UnityEngine;
public class Player : MonoBehaviour
{
private void LateUpdate()
{
RotationOffsets();
void RotationOffsets()
{
if ((Aiming || IsShooting) && !Reloading)
{
LookAt();
void LookAt()
{
Vector3 direction = Lookat - GunTip.position;
Quaternion targetRotation = Quaternion.LookRotation(direction) * Quaternion.Inverse(Quaternion.Inverse(TransformLook.rotation) * GunTip.rotation);
TransformLook.rotation = Quaternion.Lerp(TransformLook.rotation, targetRotation, 0.8f);
m.rotation);
}
}
}
void Update()
{
Move();
Shooting();
}
private void Shooting()
{
Physics.Raycast(MainCamera.position, MainCamera.forward, out RaycastHit Hit, Mathf.Infinity);
if (Hit.point != Vector3.zero) Lookat = Hit.point;
else Lookat = MainCamera.forward * 5000;
IsShooting = Input.GetMouseButton(0);
if (IsShooting && CanShoot && CurGun.MagSize > 0 && MasterShoot) Shoot(
void Shoot()
{
CurGun.MagSize--;
Bullet CurBullet = Instantiate(CurGun.BulletType, GunTip.position, GunTip.rotation);
CurBullet.rb.AddForce(CurBullet.transform.forward * CurGun.BulletVel, ForceMode.Impulse);
CurBullet.hit = Hit;
CurBullet.TimeLeft = Vector3.Distance(GunTip.position, Lookat) / CurGun.BulletVel;
if (Hit.collider != null)
{
Hit.collider.TryGetComponent<Enemy>(out Enemy TempEnemy);
CurBullet.HitEffect.material = Hit.collider.GetComponent<MeshRenderer>().material;
}
TargetRotation += new Vector3(Random.Range(-CurGun.Recoil.x, CurGun.Recoil.x), Random.Range(-CurGun.Recoil.y, CurGun.Recoil.y), CurGun.Recoil.z);
}
}
private void Move()
{
Lastvel = rb.velocity;
if (HasBounced) return;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
MouseRot = RotationThing2(Mathf.Atan2(Input.GetAxisRaw("Mouse Y") / tesstval,Input.GetAxisRaw("Mouse X")/ tesstval) * Mathf.Rad2Deg);
Jumping = Input.GetKey(KeyCode.Space);
Vector3 direction = (MainCamera.forward * vertical + MainCamera.right * horizontal).normalized * CurSpeed;
direction.y = rb.velocity.y;
if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 20f, GroundLayer) && !Grounded && rb.velocity.y < 0) StartCoroutine(CalculateTime(hit.point));
if (Landing && !Grounded && rb.velocity.y < 0) TransformLook.localRotation *= Quaternion.Euler(180, 0, 0);
if ((direction.x * direction.x >= 0.01f || direction.z * direction.z >= 0.01f) && Grounded)
{
float targetAngle = (Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg);
if(!Aiming) transform.rotation = Quaternion.Lerp(rb.transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), 0.2f);
rb.velocity = direction;
Walking = true;
}
else Walking = false;
}
private float RotationThing(float input)
{
if (input > 180f)
{
return input -= 360f;
}
else return input;
}
private float RotationThing2(float input)
{
if (input < 0)
{
return input += 360f;
}
else return input;
}
}
//HasBounced = false;
}
}