Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 ai$$anonymous$$g 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 ai$$anonymous$$g 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

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

Spawn a platform that I know a player can jump to 0 Answers

Instantiate with parent/gameObject's velocity 2 Answers

Create variable based on local axis 1 Answer

Shooting 2D - rigidbody2D.velocity not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges