- Home /
Question by
starnumstudios · Jun 18, 2018 at 05:09 PM ·
rotationtransformrotateeulerangles
My object isn't rotating to the transform of another object. How do I fix this?
I am making a game where I need an object to repeatedly rotate to the exact transfrorm of blasterTrans. however when played, when you press z it will go to the rotation of blasterTrans, but then when you press z again, it will go completely off course and not in the rotation of blasterTrans. Note: blasterTrans is a rotating object.
here's the code
<
public class Shooting : MonoBehaviour {
public Transform blasterTrans;
public Rigidbody rb;
public bool bulletMoving;
public float bulletSpeed;
void Start () {
}
void Update () {
if (Input.GetKeyDown("z"))
{
Fire();
}
if(bulletMoving == true)
{
transform.Translate(0, 0, bulletSpeed * Time.deltaTime);
}
}
public void Fire()
{
transform.Rotate(0, 0, 0);
transform.position = (new Vector3(blasterTrans.position.x, blasterTrans.position.y, blasterTrans.position.z));
transform.Rotate(new Vector3(blasterTrans.eulerAngles.x, blasterTrans.eulerAngles.y, blasterTrans.eulerAngles.z));
bulletMoving = true;
}
}
Comment
Answer by MT369MT · Jun 18, 2018 at 06:18 PM
Hi, this should work:
public void Fire()
{
transform.position = blasterTrans.position;
transform.rotation = blasterTrans.rotation;
bulletMoving = true;
}
Thank you! This worked for me, I really appreciate it.
Your answer
Follow this Question
Related Questions
Rotation Jumping values (0 to 180) 1 Answer
Object wont rotate in the opposite direction 1 Answer
Rotate and eulerAngles don't work together ?? 2 Answers
Rotating a model with increments 1 Answer