- Home /
Shooting problem the bullets fly in diffrent direction then player looks?
Hello the problem is as topic says the bullets shooted flys in difrent direction as they should so maybe some profesional unity developer could help me solve this out trying to this whole day and im out of ideas...
Shooting & movment script: using UnityEngine; using System.Collections;
public class characterMovment : MonoBehaviour {
#region --Pubs--
public bool Run {
get
{
return _run;
}
set
{
_run = value;
}
}
public GameObject ammo;
public string weaponName = "H&K";
public int weaponMinDmg = 4;
public int weaponMaxDmg = 15;
public float moveSpeed = 6.0F;
public float runSpeed = 9.0F;
public float gravity = 20.0F;
#endregion
#region --Privs--
private bool _run;
private Vector3 _moveDirection = Vector3.zero;
#endregion
// Use this for initialization
void Start () {
Run = false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.T))
{
if(_run)
{
Run = false;
}
else
{
Run = true;
}
}
CharacterController controller = GetComponent<CharacterController>();
if(controller.isGrounded)
{
_moveDirection = new Vector3(0,0,Input.GetAxis("Vertical"));
_moveDirection = transform.TransformDirection(_moveDirection);
if(!_run)
{
_moveDirection *= moveSpeed;
}
else
{
_moveDirection *= runSpeed;
}
}
_moveDirection.y -= gravity*Time.deltaTime;
controller.Move(_moveDirection*Time.deltaTime);
transform.RotateAround(new Vector3(0,Input.GetAxis("Horizontal"),0)*Time.deltaTime,Time.deltaTime);
if(Input.GetKeyDown(KeyCode.F))
{
Instantiate(ammo,transform.position,transform.rotation);
}
}
}
Bullet fly script:
using UnityEngine;
using System.Collections;
public class bulletMove : MonoBehaviour {
#region --Pubs--
public int bulletRange = 10;
public float bulletSpeed = 4;
#endregion
#region --Privs--
private Vector3 startPos;
private GameObject shooter;
#endregion
// Use this for initialization
void Start () {
shooter = GameObject.FindGameObjectWithTag("Player");
startPos = shooter.transform.position;
}
// Update is called once per frame
void Update () {
float distance = Vector3.Distance(startPos,transform.position);
Debug.Log(distance);
float move = bulletSpeed * Time.deltaTime;
transform.Translate(transform.forward * move);
if(distance >=bulletRange)
{
Destroy(gameObject);
}
}
}
Answer by selzero · Mar 04, 2013 at 06:10 PM
Maybe "forward" is not the one you are looking for, try "right" and "up" and also multiplying the vector by -1
(I cant see your game so not sure which direction you need)
Use Debug.Log() and see whats happening.
Answer by robertbu · Mar 04, 2013 at 11:23 PM
Forward is the side of your object facing positive Z. Nothing in your code rotates the forward vector of the bullet to match the target. As the last line in the Start() method of your bulletMove script, add:
transform.LookAt(startPos);
If for some reason you do not want to rotate your bullet to face your target, you could do change your translate code to:
transform.Translate((startPos - transform.position).normalized * move);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Flip over an object (smooth transition) 3 Answers
[C#]Angle of shooting equale to mouse direction 1 Answer
Wrong rotation while moving 1 Answer