2016-11-27 1 views
0

안녕하세요 내 안드로이드 프로젝트에 대한 대구 아래에 있고 위아래로 볼 때 총알 촬영 총알 촬영 방향이 잘못되었을 때 어디에 문제가 있습니까?총알 촬영 단결

여기 내 프로젝트의 비디오입니다.

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

FirstPerson

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

으로 판단 비디오, 문제는 총알이 카메라 카메라의 위치와 방향을 기준으로 발사되고 건이 아닌이 아닌 것입니다. 이것은 일반적으로 대부분의 1 인칭 슈팅 게임 (예 : 카운터 스트라이크 및 유사)에서 바람직하지만, (당신이 모바일 조이스틱 컨트롤을 사용하고 있기 때문에) 귀하의 경우에는 그렇지 않을 수도 있습니다.

이 문제는 다음 코드에 의해 발생 :

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); 

참고이되지 수도 총의 닻이 총구에없고 총알이 나오길 원하는 방향으로 벡터가 가리키고 있지 않으면 작동합니다. 이 경우 제대로 배치되고 방향이 지정된 빈 자식 GameObject를 건에 추가하려고 할 수 있습니다.