- Home /
How to set direction of velocity using Euler angles?
I have an angle between -45 (or 315) and 45 relative to the player object. I want to set the velocity and rotation of an instantiated projectile in that direction, relative to the player. If it's of any importance, this is a top down, 2D game.
Answer by misher · Feb 06, 2019 at 01:45 PM
There should be a "gun" gameobject responsible for shooting bullets, and there is a point where the bullet is instantiated, this "point" can be another gameobject with its transform pointing in the desired shooting direction (forward of the transform). When the gun shoot, it instantiates the bullet with the same exact rotation as the point, then you can apply a deviation, generate a random angle in the desired range (from -45 to 45) and add this angle to the rotation of the bullet transform. Here is an example of how you can do this:
bullet.transform.Rotate(Vector3.up, Random.Range(-45, 45), Space.Self);
Then you can apply some velocity or force in the new forward direction of the bullet
Thanks a lot, this almost works, but for some reason it's not actually deviating from the player's rotation. It's always oriented in the same exact direction.
This is within the player script:
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
{
Instantiate(bullet, transform.position, transform.rotation);
bullet.transform.Rotate(Vector3.up, Random.Range(-45, 45), Space.Self);
}
}
And this is within the bullet's script:
public Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
rb2d.AddForce(transform.up / 10);
}
Answer by mlnczk · Feb 06, 2019 at 01:04 PM
If I understood correctly you want to shoot projectiles in the direction where player is facing capped -45/45 degrees. Then try this. Make projectile prefab and attach script to it called lets say Projectile. Most work should be done with just Instantiate method where you can tell spawned object's rotation and position. In projectile script do:
void Update(){
this.transform.Translate(Vector2.up); // move object forward on Y axis, if its rotated locally should apply properly
}
And in player script you need to instatiate your projectile!
void SpawnProjectile(){
Instantiate(projectilePrefab, transform.localPosition + offset, transform.localRotation); // offset so that the projectile doesnt appear in the middle of our object but in front or something. Offset is Vector2 you can create it and adapt as you like
}
So important thing is that in your movement method where you change degrees make sure you change localRotation not just basic rotation that way your projectile will be able to adapt and Y axis will be kinda forward axis for object, not Y axis for world what is propably happening in your game right now that the object just goes straight up on Y axis instead of "foward".
This already helped a lot, but I don't want to shoot in the same direction as the player's facing. I want to shoot in a cone, where the bullet has a chance to go anywhere within that cone. So my question is, how could I modify the projectile's direction to be +- 45 degrees relative to the player's rotation?
So you want to randomise its rotation with limitation to cone? You need to use $$anonymous$$athf.Clamp method in projectile update method;
private float projectileLimitedRotation;
void Update(){
projectileLimitedRotation = $$anonymous$$athf.Clamp(projectileLimitedRotation, transform.parent.localRotation.y - 45, transform.parent.localRotation.y + 45);
transform.localRotation = Quaternion.Euler(transform.localRotation.x, projectileLimitedRotation, transform.localRotation.z);
}
This will clamp your projectile rotation and it should stick to cone you want.
Your answer
Follow this Question
Related Questions
Velocity from external objects 0 Answers
Is there a way to lock velocity? 3 Answers
Running and Jumping problems of a 2D Endless Runner 2 Answers