- Home /
AddForce to the left.
Hi, I'm quite new to unity and I am making a little game where you can shoot to the left and to the right with the arrow keys. I made the projectile go to the right with this line. projectileInstanceRight.AddForce(rightSpawnLoc.right * power);
I want to do the same to the left but ".left" doesn't exist. What's the reason it doesn't exist and how can I make it go to the left?
Answer by IggyZuk · Jul 16, 2020 at 11:00 AM
To get the left you can negate the right.
-rightSpawnLoc.right
So you'd have:
projectileInstanceRight.AddForce(-rightSpawnLoc.right * power);
Answer by unity_bTkJGG31azdikQ · Jul 16, 2020 at 02:03 PM
IggyZuk's answer seems like it will do the job, but since you said you're new, here's a little context.
It looks like rightSpawnLoc
is an instance of the Transform class, which is the component on your game object that holds spatial information, including rotation and scaling. Transform.right
is a shortcut to the Vector3 (1, 0, 0) in the game object's local reference frame. rightSpawnLoc.right
means "to the game object's right", so if you want left and right to depend on how the game object is rotated, this is a good way to do it.
Why is .left
not defined? It would be a shortcut to (-1, 0, 0), again, in the game object's local frame. The developers just didn't see fit to add that shortcut when it's the same as just multiplying .left
by -1. I'm not sure there's a deeper reason why.
It seems like maybe this is a 2D game, in which case, it probably makes sense to define the direction based on world space. To do this, you could use Vector3.right
and Vector3.left
, which are shortcuts to (1, 0, 0) and (-1, 0, 0) in world space. To throw a bit of jargon at you, these are "static" properties, meaning they're a property of the class itself, not some particular instance. So you can just use them without making a new Vector3 first. For whatever reason, the developers did see fit to define a shortcut for left here. (The fact that .left
doesn't exist in your situation is how I guessed you're using a Transform rather than a Vector3.)
Anyway, you could do
projectileInstanceLeft.AddForce(Vector3.left * power);
Based on the name of rightSpawnLoc
I'm guessing you're just using it for a starting position and could make it an instance of Vector3 in the first place. No shame in doing whatever works as you're figuring things out, but hopefully this helps clarify how some stuff works and helps your code become cleaner and more understandable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613003313im_/https://answers.unity.com/themes/thub/images/avi.jpg)