0
\$\begingroup\$

This script for drive a car by arrows. I added a gun above it to aim at target. My issue here is the rotating of my gun effected by my drive when I turn left or right. How to stop the rotation of my gun to be effected by car turns ?

using UnityEngine;
using System.Collections;

public class RearWheelDrive : MonoBehaviour {

private WheelCollider[] wheels;

public float maxAngle = 30;
public float maxTorque = 300;
public GameObject wheelShape;

public Transform target;
public Transform gun_car;

// here we find all the WheelColliders down in the hierarchy
public void Start() {

    wheels = GetComponentsInChildren<WheelCollider>();

    for (int i = 0; i < wheels.Length; ++i) 
    {
        var wheel = wheels [i];

        // create wheel shapes only when needed
        if (wheelShape != null)
        {
            var ws = GameObject.Instantiate (wheelShape);
            ws.transform.parent = wheel.transform;
        }
    }
}

public void Update()    {

    // Gun rotation
    Vector3 dir = target.position - gun_car.transform.position;
    Quaternion lookr = Quaternion.LookRotation(dir);
    Vector3 rotationy = Quaternion.Lerp(gun_car.localRotation,lookr, Time.deltaTime * 7).eulerAngles;
    gun_car.localRotation = Quaternion.Euler (0f,rotationy.y,0f);

   //    Drive 
    float angle = maxAngle * Input.GetAxis("Horizontal");
    float torque = maxTorque * Input.GetAxis("Vertical");

    foreach (WheelCollider wheel in wheels)
    {
        // a simple car where front wheels steer while rear ones drive
        if (wheel.transform.localPosition.z > 0)
            wheel.steerAngle = angle;

        if (wheel.transform.localPosition.z < 0)
            wheel.motorTorque = torque;

        // update visual wheels if any
        if (wheelShape) 
        {
            Quaternion q;
            Vector3 p;
            wheel.GetWorldPose (out p, out q);

            // assume that the only child of the wheelcollider is the wheel shape
            Transform shapeTransform = wheel.transform.GetChild (0);
            shapeTransform.position = p;
            shapeTransform.rotation = q;
        }

    }
}

}

\$\endgroup\$
1
\$\begingroup\$

If you are wanting the Gun to maintain it's rotation relative to the rotation and position of the car (a gun pointed straight ahead remains pointed straight ahead as you turn and drive), make sure that the Gun's GameObject is a child of the Car GameObject that is being turned and rotated. Inspect your GameObject Hierarchy in the scene view to verify, and if you have doubts - just take a screenshot of the fully expanded hierarchy and update your question with it. You might have a GameObject.

Conversely, if you do not want the gun to maintain it's rotation relative to the car's rotation (you turn, but the gun remains pointed at it's old target, seemingly turning to counteract the vehicle position), You will need to subtract the Car GameObject's Rotation Vector3 from the Gun's rotation to compensate for any extra rotation inherited from the parent Car GameObject.

Looking at your script, you seem to be trying to have the gun look directly at some other object. You are working with the localRotation, which would be relative to the car, not in global transform space, and that could be why your rotation isn't what is expected.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.