查看: 3031|回复: 2
打印 上一主题 下一主题

角色移动旋转问题?

[复制链接]

2206

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32449
精华
23

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-5-31 08:41:15 |只看该作者 |倒序浏览
这是我参考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);
    }
   
}




分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2206

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32449
精华
23

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-5-31 08:45:17 |只看该作者
大师们继续回帖哦!~求解决办法
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2013-5-31 21:37:24 |只看该作者
unity官方Stealth 01还没下载测试啊,貌似很大的文件
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-22 16:45 , Processed in 0.215799 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部