Unity2D shot object turns with character but shouldn't

unity 2d shoot bullet in direction
unity projectile face direction
unity 2d shooting
unity shoot projectile 2d
unity shoot in direction of player
unity shoot projectile in direction
shoot at angle unity 2d
unity shoot bullet in direction

I'm trying to learn Unity. My goal is to build a little asteroids clone.

Currently, my character is able to fly, shoot and rotate towards the mouse location. However, the bullets are somehow effected by the player turning.

Here is some sample code:

player.cs

    private void Shoot()
    {
        Instantiate(Bullet, transform);
    }

    private void FaceMouseDirection()
    {
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.back);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turningSpeed * Time.deltaTime);
    }

bullet.cs

    private GameObject parent;
    private Vector3 Force;

    // Use this for initialization
    void Start () 
    {
        parent = GameObject.Find("Player");
        Force = parent.transform.up * bulletSpeed;
    }

    void FixedUpdate()
    {
        transform.Translate(Force * Time.fixedDeltaTime);
    }
}

The bullet is being set as a child of the player. Children objects keep the same rotation as their parent.

In your bullet's Start() you will want to remove the parent from the bullet after setting the force.

Off the top of my head you should be able to do that with

transform.parent = null;

Shooting a bullet with the same rotation as the gameobject that , Shooting a bullet with the same rotation as the gameobject that spawns it. 2D At the end of that barrel I have put an empty game object which spawns the bullets, 10 |3000 characters needed characters left characters exceeded Btw I think the bullet should rather move forward than up but that depends. That's probably because you're instantiating bullets as child of the object's transform. Then, when the object rotates, the bullets rotate as well. You want to instantiate them in the world/root, or maybe in a static object at (0,0,0) if you want to group them in the hierarchy. Object.Instantiate(bullet).transform.position = base.transform.position

First, don't have the bullets be children of the player. (Don't use Instantiate(Bullet, transform);)

Second, set the rotation and position of the bullet to be the same as the player when the bullet is instantiated. ( Do use Instantiate(Bullet, transform.position, transform.rotation); )

Third, don't base the bullet's movement on the transform of the player. Just use its own transform. ( Do use Force = transform.up * bulletSpeed; instead of Force = parent.transform.up * bulletSpeed )

player.cs

    private void Shoot()
    {
        GameObject bullet = Instantiate(Bullet, transform.position, transform.rotation);
    }

    private void FaceMouseDirection()
    {
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.back);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turningSpeed * Time.deltaTime);
    }

bullet.cs

    private Vector3 Force;

    // Use this for initialization
    void Start () 
    {

        Force = -transform.right * bulletSpeed;
    }

    void FixedUpdate()
    {
        transform.Translate(Force * Time.fixedDeltaTime);
    }
}

Fourth, you should consider using a technique called object pooling for your bullets, as instantiating and destroying many near-duplicates of gameobjects is not usually very efficient.

Note: If you're using Rigidbody or Rigidbody2D on the bullets, you should rather use rb.MovePosition(transform.position + Force * Time.deltaTime); (Time.deltaTime is the same as Time.fixedDeltaTime in FixedUpdate) because it will interpolate properly for frames that render between calls to FixedUpdate.

2D look at mouse position (z rotation) [c#], mousePosition does not work (it rotates all axis, but not z) It's supposed to work , but character acts strange, it rotates from time to time, Should the name of the variable you make be changed to void Update (); {; // Distance from camera to object. Can't shoot towards mouse click point 0 Answers. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

That's probably because you're instantiating bullets as child of the object's transform. Then, when the object rotates, the bullets rotate as well.

You want to instantiate them in the world/root, or maybe in a static object at (0,0,0) if you want to group them in the hierarchy.

Object.Instantiate(bullet).transform.position = base.transform.position

[SOLVED] collider rotates when character turns/walks — Adventure , Could you post shots? In top-down 2D mode, colliders should indeed rotate. The inner workings is basically that they behave exactly as they do in� The best way to rotate a 2D character is to change the localScale from 1 to -1. This will allow you to place normal maps and have dynamic lighting affect your character. You shouldn't rotate your character while using a 2D character unless you want the paper-ish, textbook affect.

How to Fix Common Physics Problems in Your Game, Barrels are shot from the side of the scene and are hurled into the air. If you notice the opposite—that the objects move too slowly—you should scale the objects down. In adding a Rigidbody to a character, you will probably want to When writing code that moves, rotates, or scales rigidbodies, it is� Assigning this boolean was the most mystifying for me when I was learning how to work the Unity Animator. What we’re doing is sending a raycast (a raycast is simply a ray shot in a specified direction that can collect data about what it hits) in the downwards direction and thereby determining if the character is touching the ground or not.

Rotate or Aim Towards Mouse or Object in 2D - Unity [ENG], We will create a script that rotates a game object so it aims or looks at the Let a character Duration: 6:12 Posted: Jun 6, 2017 unity 2d camera follow script, add this script to camera and drag character or your object into it. simple! - unity 2d camera follow

Physics Joints in Unity 2D, Screen Shot 2015-05-26 at 2.10.57 PM Throughout this tutorial, you'll get to try out each of Unity's 2D joint types to see how they work. Play the Turn your attention back the component in the Inspector. With the connection set up and Hexagon_Djoint still selected, you should now see the two objects� I'm looking to create a basic, turn-based, grid movement system on either a square or hexagonal system. However, I'm not sure where to start. I'm just now getting into Unity and would be grateful for any resources or advice you guys can provide.

Comments
  • When a gameobject is the child of another object it uses the parents transform. Its own transform becomes relative to the parent. Just dont set the parent and you're good to go
  • Thanks alot, its working that way. The bullets are flying strange tho because they don't fly alone the green axis.. but i guess thats off topic.
  • Thanks a lot for sharing! Do you have an idea, why my bullet flight is offset? When the player looks towards north, bullts fly there. But when the player looks east, the bullets fly to the south...
  • @NullOrNotNull Because the bullets were rotated in a way that their local up wasn't the correct direction for them to travel. From your comment, it sounds like they really ought to travel in their local left direction, but without a screenshot showing their local axes and the direction they are meant to travel, it is hard to be sure. Anyway, I edited the bullet.cs part of my answer. Let me know if that changed anything for you.
  • Actually for bullets I would simply set it's rb.velocity = -transform.right * bulletSpeed; once and not use Update or FixedUpdate at all :)
  • @derHugo yeah that's probably better :)