简体   繁体   English

在自上而下的射击游戏中角色不断发抖

[英]character keeps shaking while in topdown shooter

ok well i am working on a top down shooter i have made my character look at the mouse using screentoworld view and i made my camera follow my players x, and z axis but anytime my character moves parallel from the mouse it starts shaking I think its because it looks at the mouse an the mouse real world position is dependent on the cameras position which is, but i am not sure. 好的,我正在使用自上而下的射击游戏,我已经使用screentoworld视图使角色看着鼠标,并且我的相机跟随了玩家的x和z轴,但是只要角色从鼠标平行移动,它就会开始抖动,我认为因为它看着鼠标,所以鼠标的实际位置取决于摄像头的位置,但是我不确定。

here is the playerscript the many backslashes showed other things I've tried 这是播放器脚本,许多反斜杠显示了我尝试过的其他内容

gameObject.GetComponent<Rigidbody>().velocity = new Vector3(Input.GetAxisRaw("Horizontal")*movementspeed,0,Input.GetAxisRaw("Vertical")*movementspeed);  
        if (gameObject.GetComponent<Rigidbody> ().velocity.x != 0 && gameObject.GetComponent<Rigidbody> ().velocity.z != 0) {
            gameObject.GetComponent<Rigidbody>().velocity = new Vector3(Input.GetAxisRaw("Horizontal")*movementspeeddiag ,0,Input.GetAxisRaw("Vertical")*movementspeeddiag); 
        }

    // makes vector 3 type target equall to mouse position on pixial cordinates converted in to real world coordniates
        target = camera.ScreenToWorldPoint (new Vector3(Input.mousePosition.x,Input.mousePosition.y,camera.transform.position.y - transform.position.y)); 

        //Vector3 newtarget = target - transform.position;

        //lerptarget = Vector3.Lerp (transform.eulerAngles,newtarget, 100);
        // states the vector 3 values of target 

        // makes object local z face target and iniziates up axis 

        //Quaternion rotation = Quaternion.LookRotation (newtarget, Vector3.up); 

        //transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle (transform.eulerAngles.y, rotation.eulerAngles.y, rotatespeed * Time.deltaTime);
        transform.LookAt (target,Vector3.up);

and camera script 和相机脚本

    void LateUpdate(){
        position = new Vector3 (player.transform.position.x, 10, player.transform.position.z); 
        transform.position = position;
        //transform.localPosition = Vector3.Lerp (transform.position, position, speed *Time.deltaTime); 

        transform.eulerAngles = new Vector3 (90,0,0); 
    }

Probably, the problem is that you change the velocity of rigidbody. 问题可能出在改变刚体的速度。 Becouse velocity changes applied each fixed update time interval, it doesnt match with frame updating time, and may lead to shaking. 由于速度变化是在每个固定的更新时间间隔应用的,因此它与帧更新时间不匹配,并可能导致抖动。 According to unity docs you should try to avoid directly changes of velocity. 根据unity docs,您应该尝试避免直接改变速度。 ( http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html ) http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

I'd suggest you to you use MovePosition alongside with Lerp function to implement player movement. 我建议您同时使用MovePosition和Lerp函数来实现玩家移动。 See this: http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html 请参阅: http : //docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM