- Home /
This question is no longer relevant.
Rotate pieces of model to look at player
I have a artillery-type model in my game, and i want one group of pieces to rotate left and right to look at the player, and a group inside that group to also rotate up and down to point at the player. I have a script made, but it won't let me pick the right direction to be forward.
using UnityEngine;
using System.Collections;
public class Aim : MonoBehaviour {
public Vector3 LeftRightForward;
public Vector3 UpDownForward;
public bool LeftRight;
public bool UpDown;
GameObject Player;
void Start()
{
Player = GameObject.Find("Player");
}
void Update()
{
if (LeftRight)
{
GetComponent<Rigidbody>().rotation = Quaternion.LookRotation(new Vector3(Player.transform.position.x, transform.position.y, Player.transform.position.z) - transform.position, LeftRightForward);
}
if (UpDown)
{
GetComponent<Rigidbody>().rotation = Quaternion.LookRotation(Player.transform.position - transform.position, UpDownForward);
}
}
}
(The reason for so many public variables is so i could find out which direction was the right forward for the model from the editor.)
Here are pictures of the model: Does anyone know what is wrong?
P.S. The Up-Down and Left-Right have the script attached with the correct bool set to true. These pictures are slightly old
Answer by oren_danniel · Nov 08, 2016 at 03:20 PM
Have you tried:
//for instant rotation
transform.LookAt(transform);
//smooth rotation
transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(target.position - this.transform.position), speed * Time.deltaTime);//in here you can edit the vector target so it would work only on the x axis for exmp
the wrong side of the model faces the player when I do this. Is there a way to fix that?
Which axis X, Y, Z? What are you using lookAt or rotation?
Regardless try to put a $$anonymous$$us on the wrong side!
I'm doing LookAt(), and i have tried every combination of 1's and -1's in the new Vector3 as the second field in the LookAt(transform, Vector3 up) and nothing points my model in the right direction. Here is a video and picture showing what happens and what I want to happen:
Video: https://drive.google.com/open?id=0B_HzgsC9lP3nQXBXNU1NaVVtQjQ