Vector rotation

Hello, dear community! I have a 2D game from the top view. I would like to make a character shooting forward one projectile, one projectile 45° to the left and another projectile 45° to the right.

I know how to correctly spawn the first projectile with the right position and direction. But 2 other projectiles do not work as intended - they seem to spawn just at the top of the first one.

I am leaving a script and a picture for reference. Do you guys have an idea, what may be the problem? Or maybe there is another way to rotate these 2 projectile vectors +/- some degrees from the main vector? Any help is very appreciated!

 function Update() {

   		//if a player is moving
	    if(transform.position != old_position)
	    newVector = transform.position - old_position;
	    var coordinates = GameObject.Find("professor").transform.position;
		    	//if a player decides to shoot while moving
			    if (Input.GetMouseButtonDown(0))
			    var projectile: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);    
			    var force =10;
			    projectile.rigidbody2D.AddForce(newVector.normalized * force / Time.fixedDeltaTime);
	    plus45vector = Quaternion.AngleAxis(45, Vector2.up)* newVector;
		       var projectile2: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
		        projectile2.rigidbody2D.AddForce(plus45vector.normalized  * force / Time.fixedDeltaTime);
			    minus45vector = Quaternion.AngleAxis(-45, Vector2.up)* newVector;
		       var projectile3: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
		        projectile3.rigidbody2D.AddForce(minus45vector  * force / Time.fixedDeltaTime);
	    old_position = transform.position; 

Awesome picture.

You are spawning all of the projectiles at the Vector3 coordinates, so yes they all will spawn in the same place.

If you want to spawn them in different places you have to calculate different positions.

A mathfree approach would be to create spawnpoints as children of the gameObject.

Is this what you mean, or is your problem actually that the shots arent flying the right way?


Got this working now. The major issue is the quaternion multiplication.
projectile.rigidbody2D.velocity = newVector * Quaternion.AngleAxis(45f, Vector3.forward) * Vector3.up * projectileSpeed;
This should do the trick.

Rigidbody2D bullet = Instantiate() I couldnt get to work in c#, only passing a Transform did work for me, so maybe thats another issue.

As was mentioned the use of fixedDeltatime is illogical, since its a constant(see manual). So delete that. I wouldnt see sense in using Time.deltatime also. Because we always want to apply the same force, and not more if more time has passed.

The projectiles spawning in the same place is also going to cause issues, if they collide with eachother. As mentioned by robertbu.

Also I think its better practice to directly set the velocity of the projectiles instead of adding a force, since this achieves more directly what we want to do. rigidbody2D.velocity = …

Lastly, because I believe this kind of information is always good to repeat, using Gameobject.Find in timecritical operations is not so good, because its slow.

Thank you again for trying to help, I upvoted you, unfortunatelly don’t know how to commend robertbu.

In the line

var projectile2: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
projectile2.rigidbody2D.AddForce(plus45vector.normalized  * force / Time.fixedDeltaTime);

I’m trying to substitute according to your remarks:

 var projectile2: Rigidbody2D = Instantiate (rocket, coordinates, Quaternion.identity);  
 projectile2.rigidbody2D.velocity = newVector * Quaternion.AngleAxis(45f, Vector2.forward) * Vector2.up * force;  
And the Unity shows an error: "Operator '*' cannot be used with a left hand side of type 'UnityEngine.Vector2 and a right hand side of type UnityEngine.Quaternion." Removing newVector from the equation removes the error. And spawns the projectile without any velocity at 0-0-0.

Anyway, thank you for help guys! I am pleased to received answers so quickly. Thank you.