I’m trying to make a ball come out of a cannon in a projectile motion. The cannon is supposed to shoot the ball at player object in the scene. The following code is attached to the cannonBall prefab.
void Awake()
{
this.GetComponent<Rigidbody> ().AddRelativeForce (new Vector3 (0, GetRandomValue (), GetRandomValue ()));
}
float GetRandomValue()
{
return Random.Range (300.0f, 700.0f);
}
The following code is attached to the cannon.
void Update()
{
this.transform.LookAt (player);
GameObject obj = Instantiate (cannonBall, this.transform.position, this.transform.rotation) as GameObject;
}
The cannonBall clone instantiated in the above code is instantiated at the position of the cannon, but it’s rotation is not equivalent to that of the cannon’s, but of the cannonBall prefab’s.
When I use the following code the cannonBall clone is moving in the direction of the player, but it is also changing the rotation of the prefab.
cannonBall.transform.rotation = this.transform.rotation;
Why is the instantiate method not using the cannon’s rotaion? Is it not possible to change to rotation of clone without changing the prefab’s rotation?