- Home /
transform.Rotate doesn't work inside Start() method
I'm trying to write a script that will launch projectiles using RigidBody.AddRelativeForce. I also want to have random rotation around vertical axis (set before applying force), so that each projectile can have slightly different trajectory. Simplified version of my code looks like this
using UnityEngine;
public class BalisticProjectile : MonoBehaviour
{
public RigidBody rb;
public float speed = 400.0f;
public float yOffset = 15.0f;
void Start()
{
Random.seed = System.DateTime.Now.Millisecond;
transform.Rotate(new Vector3(0.0f, Random.Range(-yOffset, yOffset), 0.0f));
rb.AddRelativeForce(Vector3.forward * speed);
}
}
Basically, upon instantiation (which is handled by another script), projectile would fly forward until it falls to the ground. Problem is, each projectile launched this way have exactly the same trajectory. It seems like transform.Rotate has no effect on the object. I know there are other (probably better) ways to randomize the trajectory, but I want to know why is it not working in this particular case. Are statements inside Start() method executed in correct order or is AddRelativeForce somehow executed before transform.Rotate?
Could be an issue with the coordinate systems. Try substituting with AddForce() and transform.forward ins$$anonymous$$d.
Answer by Tsaras · Feb 25, 2019 at 05:24 PM
Vector3.forward is a constant vector pointing to (0,0,1) the world space system. So you are in effect sending the bullets in the same direction. Use transform.forward to get the corresponding vector in local space.
edit: correction, transform.forward is still in world space.
It worked. I substituted line 12 with
rb.AddForce(transform.forward * speed);
Projectiles are flying in the direction the weapon is pointing to, and they have random Y rotation. The answer given by @fhilleri is also working fine.
Answer by fhilleri · Feb 25, 2019 at 05:33 PM
Try with that :
void Start()
{
Random.seed = System.DateTime.Now.Millisecond;
transform.Rotate(new Vector3(0.0f, Random.Range(-yOffset, yOffset), 0.0f));
rb.AddForce(transform.TransformDirection(Vector3.forward) * speed);
}
Answer by eddiesd89 · Feb 25, 2019 at 07:19 PM
Both answers working correctly. What bothers me is that my script also fired the missiles in the direction the weapon was pointing to, but without randomly changing their trajectory due to difference in Y rotation. I fired several projectiles into the air, paused the frame and checked their rotation in the scene view. Each had different Y rotation, meaning that transform.Rotate was indeed applied, but somehow it didn't have effect on how AddRelativeForce was throwing them. Strangely, if I modify the code this way
using UnityEngine;
public class BalisticProjectile : MonoBehaviour
{
public RigidBody rb;
public float speed = 400.0f;
public float yOffset = 15.0f;
private bool isFired = false;
void Start()
{
Random.seed = System.DateTime.Now.Millisecond;
transform.Rotate(new Vector3(0.0f, Random.Range(-yOffset, yOffset), 0.0f));
}
void Update()
{
if(!isFired)
{
rb.AddRelativeForce(Vector3.forward * speed);
isFired = true;
}
}
}
executing transform.Rotate in the Start() function, and use Update() function to AddRelativeForce only once, the code seem to perform exactly like the ones given by @Tsaras and @fhilleri
Your answer
Follow this Question
Related Questions
Recoil function: smoothly rotation 1 Answer
Excluding Y axis rotation from a vector3 variable? 0 Answers
Stopping transform.Rotate on an exact rotation value 1 Answer
Is it ok to mix transform.Translate and rigidbody.AddForce 0 Answers
Ball moving much faster forward when there is horizontal input 0 Answers