2016-11-27 76 views
0

您好我有我的android項目鱈魚,當我查找上下和射擊子彈子彈射擊方向是錯的問題出在哪裏?子彈射擊統一

這裏是我的項目vidoe。

http://www.mediafire.com/file/9xhxvyxds4cyego/2016-11-27_15-07-39.mp4

第一人稱

using System; 
using UnityEngine; 
using UnityStandardAssets.CrossPlatformInput; 
using UnityStandardAssets.Utility; 
using Random = UnityEngine.Random; 

namespace UnityStandardAssets.Characters.FirstPerson 
{ 
    [RequireComponent(typeof (CharacterController))] 
    [RequireComponent(typeof (AudioSource))] 
    public class FirstPersonController : MonoBehaviour 
    { 
     [SerializeField] private bool m_IsWalking; 
     [SerializeField] private float m_WalkSpeed; 
     [SerializeField] private float m_LookSpeed=4; 

     [SerializeField] private float m_RunSpeed; 
     [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten; 
     [SerializeField] private float m_JumpSpeed; 
     [SerializeField] private float m_StickToGroundForce; 
     [SerializeField] private float m_GravityMultiplier; 
     [SerializeField] private MouseLook m_MouseLook; 
     [SerializeField] private bool m_UseFovKick; 
     [SerializeField] private FOVKick m_FovKick = new FOVKick(); 
     [SerializeField] private bool m_UseHeadBob; 
     [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob(); 
     [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob(); 
     [SerializeField] private float m_StepInterval; 
     [SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from. 
     [SerializeField] private AudioClip m_JumpSound;   // the sound played when character leaves the ground. 
     [SerializeField] private AudioClip m_LandSound;   // the sound played when character touches back on ground. 

     private Camera m_Camera; 
     private bool m_Jump; 
     private float m_YRotation; 
     private Vector2 m_Input; 
     private Vector3 m_MoveDir = Vector3.zero; 
     private CharacterController m_CharacterController; 
     private CollisionFlags m_CollisionFlags; 
     private bool m_PreviouslyGrounded; 
     private Vector3 m_OriginalCameraPosition; 
     private float m_StepCycle; 
     private float m_NextStep; 
     private bool m_Jumping; 
     private AudioSource m_AudioSource; 
     public GameObject AK; 
     // Use this for initialization 
     private void Start() 
     { 
      Instantiate(AK); 

      m_CharacterController = GetComponent<CharacterController>(); 
      m_Camera = Camera.main; 
      m_OriginalCameraPosition = m_Camera.transform.localPosition; 
      m_FovKick.Setup(m_Camera); 
      m_HeadBob.Setup(m_Camera, m_StepInterval); 
      m_StepCycle = 0f; 
      m_NextStep = m_StepCycle/2f; 
      m_Jumping = false; 
      m_AudioSource = GetComponent<AudioSource>(); 
      m_MouseLook.Init(transform , m_Camera.transform); 
     } 


     // Update is called once per frame 
     private void Update() 
     { 
      RotateView(); 
      // the jump state needs to read here to make sure it is not missed 
      if (!m_Jump) 
      { 
       m_Jump = CrossPlatformInputManager.GetButtonDown("Jump"); 
      } 

      if (!m_PreviouslyGrounded && m_CharacterController.isGrounded) 
      { 
       StartCoroutine(m_JumpBob.DoBobCycle()); 
       PlayLandingSound(); 
       m_MoveDir.y = 0f; 
       m_Jumping = false; 
      } 
      if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded) 
      { 
       m_MoveDir.y = 0f; 
      } 

      m_PreviouslyGrounded = m_CharacterController.isGrounded; 
     } 


     private void PlayLandingSound() 
     { 
      m_AudioSource.clip = m_LandSound; 
      m_AudioSource.Play(); 
      m_NextStep = m_StepCycle + .5f; 
     } 


     private void FixedUpdate() 
     { 
      float speed; 
      GetInput(out speed); 
      // always move along the camera forward as it is the direction that it being aimed at 
      Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x; 

      // get a normal for the surface that is being touched to move along it 
      RaycastHit hitInfo; 
      Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo, 
           m_CharacterController.height/2f); 
      desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized; 

      m_MoveDir.x = desiredMove.x*speed; 
      m_MoveDir.z = desiredMove.z*speed; 


      if (m_CharacterController.isGrounded) 
      { 
       m_MoveDir.y = -m_StickToGroundForce; 

       if (m_Jump) 
       { 
        m_MoveDir.y = m_JumpSpeed; 
        PlayJumpSound(); 
        m_Jump = false; 
        m_Jumping = true; 
       } 
      } 
      else 
      { 
       m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime; 
      } 
      m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime); 

      ProgressStepCycle(speed); 
      UpdateCameraPosition(speed); 
     } 


     private void PlayJumpSound() 
     { 
      m_AudioSource.clip = m_JumpSound; 
      m_AudioSource.Play(); 
     } 


     private void ProgressStepCycle(float speed) 
     { 
      if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0)) 
      { 
       m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))* 
          Time.fixedDeltaTime; 
      } 

      if (!(m_StepCycle > m_NextStep)) 
      { 
       return; 
      } 

      m_NextStep = m_StepCycle + m_StepInterval; 

      PlayFootStepAudio(); 
     } 


     private void PlayFootStepAudio() 
     { 
      if (!m_CharacterController.isGrounded) 
      { 
       return; 
      } 
      // pick & play a random footstep sound from the array, 
      // excluding sound at index 0 
      int n = Random.Range(1, m_FootstepSounds.Length); 
      m_AudioSource.clip = m_FootstepSounds[n]; 
      m_AudioSource.PlayOneShot(m_AudioSource.clip); 
      // move picked sound to index 0 so it's not picked next time 
      m_FootstepSounds[n] = m_FootstepSounds[0]; 
      m_FootstepSounds[0] = m_AudioSource.clip; 
     } 


     private void UpdateCameraPosition(float speed) 
     { 
      Vector3 newCameraPosition; 
      if (!m_UseHeadBob) 
      { 
       return; 
      } 
      if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded) 
      { 
       m_Camera.transform.localPosition = 
        m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude + 
             (speed*(m_IsWalking ? 1f : m_RunstepLenghten))); 
       newCameraPosition = m_Camera.transform.localPosition; 
       newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset(); 
      } 
      else 
      { 
       newCameraPosition = m_Camera.transform.localPosition; 
       newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset(); 
      } 
      m_Camera.transform.localPosition = newCameraPosition; 
     } 


     private void GetInput(out float speed) 
     { 
      // Read input 
      float horizontal = CrossPlatformInputManager.GetAxisRaw("Horizontal"); 
      float vertical = CrossPlatformInputManager.GetAxisRaw("Vertical"); 

      bool waswalking = m_IsWalking; 

#if !MOBILE_INPUT 
      // On standalone builds, walk/run speed is modified by a key press. 
      // keep track of whether or not the character is walking or running 
      m_IsWalking = !Input.GetKey(KeyCode.LeftShift); 
#endif 
      // set the desired speed to be walking or running 
      speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed; 
      m_Input = new Vector2(horizontal, vertical); 

      // normalize input if it exceeds 1 in combined length: 
      if (m_Input.sqrMagnitude > 1) 
      { 
       m_Input.Normalize(); 
      } 

      // handle speed change to give an fov kick 
      // only if the player is going to a run, is running and the fovkick is to be used 
      if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0) 
      { 
       StopAllCoroutines(); 
       StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown()); 
      } 
     } 


     private void RotateView() 
     { 
      #if !MOBILE_INPUT 

      m_MouseLook.LookRotation (transform, m_Camera.transform); 
      m_Camera.transform.localEulerAngles = new Vector3 (-m_MouseLook.y, m_Camera.transform.localEulerAngles.y, 
                   m_Camera.transform.localEulerAngles.z); 
      transform.localEulerAngles = new Vector3 (0, m_MouseLook.x, 0); 

#else 
      Vector2 m_MouseLook= new Vector2(CrossPlatformInputManager.GetAxisRaw("HorizontalLook") 
               ,CrossPlatformInputManager.GetAxisRaw("VerticalLook")); 
      float Camx=m_Camera.transform.localEulerAngles.x; 

      if((Camx>280 && Camx<=360) || 
       (Camx >=0 && Camx<80) || 
       (Camx>=80 && Camx<180 && m_MouseLook.y>0) || 
       (Camx>180 && Camx<=280 && m_MouseLook.y<0)) 
      { 
       m_Camera.transform.localEulerAngles += new Vector3 (-m_MouseLook.y*m_LookSpeed, m_Camera.transform.localEulerAngles.y, 
                    m_Camera.transform.localEulerAngles.z); 
      // AK.transform.localEulerAngles=m_Camera.transform.localEulerAngles; 
      } 


      transform.localEulerAngles += new Vector3 (0, m_MouseLook.x*m_LookSpeed, 0); 

#endif 

     m_YRotation = m_MouseLook.y; 

     } 


     private void OnControllerColliderHit(ControllerColliderHit hit) 
     { 
      Rigidbody body = hit.collider.attachedRigidbody; 
      //dont move the rigidbody if the character is on top of it 
      if (m_CollisionFlags == CollisionFlags.Below) 
      { 
       return; 
      } 

      if (body == null || body.isKinematic) 
      { 
       return; 
      } 
      body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse); 
     } 
    } 
} 

操縱桿鱈------------- ///////////////

using System; 
using UnityEngine; 
using UnityEngine.EventSystems; 

namespace UnityStandardAssets.CrossPlatformInput 
{ 
    public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler 
    { 
     public enum AxisOption 
     { 
      // Options for which axes to use 
      Both, // Use both 
      OnlyHorizontal, // Only horizontal 
      OnlyVertical // Only vertical 
     } 

     public int MovementRange = 100; 
     public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use 
     public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input 
     public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input 

     Vector3 m_StartPos; 
     bool m_UseX; // Toggle for using the x axis 
     bool m_UseY; // Toggle for using the Y axis 
     CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input 
     CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input 

     void OnEnable() 
     { 
      CreateVirtualAxes(); 
     } 

     void Start() 
     { 
      m_StartPos = transform.position; 
     } 

     void UpdateVirtualAxes(Vector3 value) 
     { 
      var delta = m_StartPos - value; 
      delta.y = -delta.y; 
      delta /= MovementRange; 
      if (m_UseX) 
      { 
       m_HorizontalVirtualAxis.Update(-delta.x); 
      } 

      if (m_UseY) 
      { 
       m_VerticalVirtualAxis.Update(delta.y); 
      } 
     } 

     void CreateVirtualAxes() 
     { 
      // set axes to use 
      m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal); 
      m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical); 

      // create new axes based on axes to use 
      if (m_UseX) 
      { 
       m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName); 
       CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis); 
      } 
      if (m_UseY) 
      { 
       m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName); 
       CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis); 
      } 
     } 


     public void OnDrag(PointerEventData data) 
     { 
      Vector3 newPos = Vector3.zero; 

      if (m_UseX) 
      { 
       int delta = (int)(data.position.x - m_StartPos.x); 
       delta = Mathf.Clamp(delta, - MovementRange, MovementRange); 
       newPos.x = delta; 
      } 

      if (m_UseY) 
      { 
       int delta = (int)(data.position.y - m_StartPos.y); 
       delta = Mathf.Clamp(delta, -MovementRange, MovementRange); 
       newPos.y = delta; 
      } 
      transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z); 
      UpdateVirtualAxes(transform.position); 
     } 


     public void OnPointerUp(PointerEventData data) 
     { 
      transform.position = m_StartPos; 
      UpdateVirtualAxes(m_StartPos); 
     } 


     public void OnPointerDown(PointerEventData data) { } 

     void OnDisable() 
     { 
      // remove the joysticks from the cross platform input 
      if (m_UseX) 
      { 
       m_HorizontalVirtualAxis.Remove(); 
      } 
      if (m_UseY) 
      { 
       m_VerticalVirtualAxis.Remove(); 
      } 
     } 
    } 
} 

射擊子彈

#pragma strict 
//--------------------------------------------------------- 
//--------------------------------------------------------- 
var Mig29_Fulcrum:GameObject; 
var AK:Rigidbody; 
var muzzel:GameObject; 
var bullet:Rigidbody; 
var Speed:int=700; 
private var Fire:int=0; 
private var Shooting:boolean =false; 
private var gun:GameObject; 

private var fireDelay:float = 0f; // the delay between shots 

function Start() { 

gun=GameObject.FindWithTag("MyGun"); 

//var AkGun:Rigidbody=Instantiate(AK,this.transform.position,this.transform.rotation); 
//var b:Rigidbody=Instantiate(bullet,this.transform.position,this.transform.rotation); 

} 

function Update() { 


if(Input.GetButtonDown("Fire2")) 
{ 
//gun.GetComponent.<Animation>().Play("Jump"); 

Shooting=true; 
} 

if(Input.GetButtonDown("Fire1")) 
{ 
Shooting=false; 

} 
if(Shooting) 
{ 
    AutoFire(0.08f); 
} 

} 

    function FireWeapon() 
    { 
     Debug.Log("Fire!!!"); 

    GetComponent.<AudioSource>().Play(); 
    gun.GetComponent.<Animation>().Play("GunFire"); 

var m:GameObject=Instantiate(muzzel,this.transform.position,this.transform.rotation); 
m.transform.Rotate(Vector3(-90,0,0)); 
m.transform.Rotate(Vector3(0,Random.Range(0,180),0)); 
Destroy(m,0.015); 

var b:Rigidbody=Instantiate(bullet,this.transform.position,this.transform.rotation); 
b.AddForce(this.transform.forward*Speed); 


    } 

    // The function below activates the FireWeapon function 
    function AutoFire(fireRate:float) 
    { 
     if (fireDelay < fireRate) 
     { 
      fireDelay += Time.deltaTime; 
     } 

     if (fireDelay >= fireRate) 
     { 
      FireWeapon(); 
      fireDelay = 0f; 
     } 


    } 

回答

0

通過評審視頻中,問題在於你的子彈是根據相機的位置和方向發射的,而不是槍的。這在大多數第一人稱射擊遊戲(例如Counter-Strike和類似遊戲)中通常是可取的,但在您的情況下可能不會(因爲您正在使用移動遊戲杆控件)。

此行爲是由下面的代碼引起的:

var b:Rigidbody=Instantiate(bullet,this.transform.position,this.transform.rotation); 
b.AddForce(this.transform.forward*Speed); 

由於您使用的位置和this.transform旋轉,腳本是指改變你連接的腳本到相機。

如果你希望你的遊戲不同的行爲,你應該考慮指槍的變換組件,而不是(方便起見,在套管存儲在變量gun):

var b:Rigidbody=Instantiate(bullet,gun.transform.position,gun.transform.rotation); 
b.AddForce(gun.transform.forward*Speed); 

請注意,這可能不是如果你的槍的錨點不在槍口上,它的forward矢量不指向你想讓子彈彈出的方向。在這種情況下,您可能需要向槍中添加一個空的兒童GameObject,該兒童的GameObject已正確定位並定位。