- Home /
Rotating my mesh towards other mesh
I have a problem rotating my mesh towards other mesh. my mesh is a cannon - it's face is where the cannons are going from. and i want it to rotate so it would face a cube. but it doesnt work. I tried everything.
Thank you for your help.
here is the relevant cannon script: (head is the part that should rotate)
public float turningSpeed=5f;
public Transform target;
public Transform head;
private Quaternion rotation;
// Update is called once per frame
void Update () {
if (target) {
calcAimPosition (target.position);
head.rotation = Quaternion.Lerp(head.rotation, rotation, Time.deltaTime * turningSpeed);
}
}
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy") {
target = other.gameObject.transform;
}
}
public void OnTriggerExit(Collider other)
{
if (other.gameObject.transform == target)
target = null;
}
public void calcAimPosition(Vector3 targetPos)
{
Vector3 aimPoint = new Vector3(targetPos.x -head.position.x, targetPos.y - head.position.y, targetPos.z - head.position.z);
rotation = Quaternion.LookRotation (aimPoint-transform.position);
}
Comment
Yes quaternion's look at function will do the job, you could even subtract their positions to get the look at direction vector.
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to rotate object not from it's center point but from it's another point? 1 Answer
rotation relative to world / starting position 1 Answer
How to tilt the gameobject based of Input.Acceleration? 0 Answers
Temperature Script? 3 Answers