纳金网

标题: 角色移动旋转问题? [打印本页]

作者: 比巴卜    时间: 2013-5-31 08:41
标题: 角色移动旋转问题?
这是我参考unity官方Stealth 01的角色移动脚本,
我是将它改成顺着摄影机的座标作角色移动,
可是遇到一个大问题,当我移动的时候是可以顺着摄影机的座标座移动,
可是按键一放开,出现这样的讯息:“Look rotation viewing vector is zero”
角色又旋转到原本方向......(应该是Rotating()的问题,可是不知道怎么改)
请问我该怎么改让他停下来依然在原来的方向?
恳求指导


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEngine;
using System.Collections;
     
public class PlayerMovement : MonoBehaviour
{
    public AudioClip shoutingClip;      // Audio clip of the player shouting.
    public float turnSmoothing = 15f;   // A smoothing value for turning the player.
    public float speedDampTime = 0.1f;  // The damping for the speed parameter
         
         
    private Animator anim;              // Reference to the animator component.
    private HashIDs hash;           // Reference to the HashIDs.
         
         
    void Awake ()
    {
        // Setting up the references.
        anim = GetComponent<Animator>();
        hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
            
        // Set the weight of the shouting layer to 1.
        anim.SetLayerWeight(1, 1f);
    }
         
         
    void FixedUpdate ()
    {
        // Cache the inputs.
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        bool dodge = Input.GetButton("Dodge");
            
        MovementManagement(h, v, dodge);
    }
         
         
    void Update ()
    {
        // Cache the attention attracting input.
        bool jump = Input.GetButtonDown("Jump");
        bool attack = Input.GetButtonDown ("Fire1");
            
        // Set the animator shouting parameter.
        anim.SetBool(hash.jumpingBool, jump);
        anim.SetBool(hash.attackingBool, attack);
    }
         
         
    void MovementManagement (float horizontal, float vertical, bool dodging)
    {
        // Set the sneaking parameter to the sneak input.
        anim.SetBool(hash.dodgingBool, dodging);
            
        // If there is some axis input...
        if(horizontal != 0f || vertical != 0f)
        {
            // ... set the players rotation and set the speed parameter to 5.5f.
            Rotating(horizontal, vertical);
            anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
        }
        else
            // Otherwise set the speed parameter to 0.
            anim.SetFloat(hash.speedFloat, 0);
    }
         
         
    void Rotating (float horizontal, float vertical)
    {
        float v = Input.GetAxisRaw("Vertical");
        float h = Input.GetAxisRaw("Horizontal");
            
        Transform cameraTransform = Camera.main.transform;
        Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
        forward.y = 0;
        forward = forward.normalized;
            
        Vector3 right = new Vector3(forward.z, 0, -forward.x);
            
        Vector3 targetDirection = h * right + v * forward;
            
        // Create a new vector of the horizontal and vertical inputs.
        //targetDirection = new Vector3(horizontal, 0f, vertical);
            
        // Create a rotation based on this new vector assuming that up is the global y axis.
        Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
            
        // Create a rotation that is an increment closer to the target rotation from the player's rotation.
        Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
            
        // Change the players rotation to this new rotation.
        rigidbody.MoveRotation(newRotation);
    }
   
}





作者: 比巴卜    时间: 2013-5-31 08:45
大师们继续回帖哦!~求解决办法
作者: 王者再临    时间: 2013-5-31 21:37
unity官方Stealth 01还没下载测试啊,貌似很大的文件




欢迎光临 纳金网 (http://c-www.narkii.com/club/) Powered by Discuz! X2.5