I’ve been using the following script to randomly rotate an object on instantiation and then have it travel in a straight line:
using System.Collections;
public class shrapnel : MonoBehaviour {
public float speed = 10;
Vector3 randomDirection;
Vector3 position;
// Use this for initialization
void Start () {
//Setting the angle and rotating the shrapnel, ready to move
float randomAngle = Random.Range (-35, 35);
transform.Rotate(0f, 0f, randomAngle);
}
// Update is called once per frame
void Update () {
//Shrapnel travels in direction of local rotation, in opposite direction to local up
transform.Translate((transform.up * -1) * speed * Time.deltaTime, Space.Self);
//Destroy when off the screen
if (transform.position.y < 0){
Destroy(this.gameObject);
}
if (transform.position.x < 0 || transform.position.x > Screen.width)
Destroy(this.gameObject);
//Apply spinning motion
}
}
It’s a 2D game.
But since I’m using the local direction to move, if I use transform.Rotate to make the shrapnel spin on its axis whilst it goes in a straight line, it changes the trajectory of the object.
I’ve tried using rigidbody.AddTorque since I thought the torque axes would be separate to rotation axes, but it’s having no effect (a rigidbody component HAS been attached).
Does anyone have any ideas what I could do here?
Many thanks in advance.