- Home /
The question is answered, right answer was accepted
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;
}
}
}
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.
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.
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!
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?
Follow this Question
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