• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 20, 2014 at 01:22 AM by aronatvw for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by aronatvw · Jul 19, 2014 at 02:56 AM · instantiatevector3velocityshootingmath

Please help with some Vector math! :-)

I am making a 2D shooter game similar to Metal Slugs I guess. I want the mouse to control the aim direction.

I accomplished this well by instantiating bullets towards the screen to world position of the mouse. My only problem now is the velocity of the bullet changes depending on the distance of the mouse from the character.

I am trying to figure out the simplest way to do this and would greatly appreciate any insight. I am guessing I need to use the angle between the mouse position and the character, and magnitude to create a new vector.

 // Gun Control Code
 
 using UnityEngine;
 using System.Collections;
 
 public class GunContLookAt : MonoBehaviour {
 
     public GameObject barrel;
     public Vector3 pubMousePos;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         Vector3 mousePos2 = new Vector3(mousePos.x, mousePos.y, barrel.transform.position.z);
 
         Vector3 test = new Vector3(barrel.transform.position.x * 2, barrel.transform.position.y * 2, barrel.transform.position.z);
 
         Debug.DrawLine(barrel.transform.position, test);
 
         pubMousePos = mousePos2;
 
 
 
         barrel.transform.LookAt(mousePos2);
 
 
 
     }
 }
 
 // Shoot Code
 
 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
 
     private GunContLookAt gunCLA;
 
     public GameObject gunPivot;
     public GameObject gunPos;
     public GameObject barrel;
 
     public Rigidbody ammo;
     public float vel = 10;
 
     // Use this for initialization
     void Start () {
 
         gunCLA = gunPivot.GetComponent<GunContLookAt>();
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetKeyDown(KeyCode.I))
         {
             Debug.Log("Position = " + barrel.transform.position + "\n Local Position is = " + barrel.transform.localPosition);
             
         }
     
         if(Input.GetButton("Fire1"))
         {
 
             Rigidbody clone;
             clone = Instantiate(ammo, barrel.transform.position, barrel.transform.rotation)as Rigidbody;
 
             Vector3 targDir = gunCLA.pubMousePos;
             Vector3 forward = gunPos.transform.forward;
             float angle = Vector3.Angle(targDir, forward);
 
             clone.velocity = transform.TransformDirection((gunCLA.pubMousePos - barrel.transform.position) * vel).normalized;
 
         }
 
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Huacanacha · Jul 19, 2014 at 04:06 AM 1
Share

Your current velocity calculation will always result in an initial speed of 1, as you normalise the velocity vector. What you surely want to do is normalise the direction then multiply by speed (which as you have it currently can be a public class variable that can be set in the Inspector).

 clone.velocity = (gunCLA.pub$$anonymous$$ousePos - barrel.transform.position).normalized * vel;

You shouldn't need to use TransformDirection as your position Vectors appear to already be in World Space coordinates.

Do your bullets travel slower the further they go, but have the same speed out of the barrel? If so you may have drag on the Ammo rigid body, or some other physical forces may be affecting them. You can use a $$anonymous$$inematic rigid body if the bullets shouldn't have external physics forces applied to them.

avatar image aronatvw · Jul 20, 2014 at 01:06 AM 0
Share

Sorry I posted that code before I changed normalize back because your right when normalized it stops at the barrel. Also what is happening is when I am further away the bullet is faster and closer the bullet is slower.

avatar image aronatvw · Jul 20, 2014 at 02:02 AM 0
Share

Actually update, the bullets are traveling well now but I also noticed when getting really close to the instantiate vector the change velocity!

Also the problem with the ammo traveling backwards when aiming at the character themselves I solved with a boolean and mouse enter/ mouse exit. I think it's fair to prevent the gun from firing when aiming it at yourself!

1 Reply

  • Sort: 
avatar image
0

Answer by aronatvw · Jul 20, 2014 at 01:21 AM

sweet man I fixed it with this line!

clone.velocity = (gunCLA.pubMousePos - barrel.transform.position).normalized * vel;

Now I just need to prevent the bullets from shooting towards the character when the mouse is on him/her.

Also different issue my side issue, my barrel is a child of the gun and when I aim to the right of the character the barrel is fine z = 0, but when aiming to the left the barrel like flips to the other side of the gun and it's 'z' position changes! Any ideas?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating Weirdly 1 Answer

Rigidbody.velocity works diffrent on cloned object 0 Answers

instantiate object and align with object surface 2 Answers

Vector3 and velocityQuestion 1 Answer

how to display grid that display whole number from 1 to 9? how to detect player touch the grid? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges