首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何让一个球员在击球时不会“出局”?

我如何让一个球员在击球时不会“出局”?
EN

Stack Overflow用户
提问于 2016-08-30 00:38:25
回答 1查看 121关注 0票数 0

因此,为了统一,我一直在努力让这个第三人称控制器脚本工作,到目前为止,它是按照我希望的方式工作的,只是现在我被困在如何让他同时向前和向左移动(或其他方向)的问题上,因为现在他开始崩溃了。

移动代码:

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;
public class Character_Controler : MonoBehaviour
{
    [SerializeField]
    public float speed = 1f;
    // the world-relative desired move direction, calculated from the camForward and user input.
    private bool canJump = false;
    Vector3 movement;
    Rigidbody playerRbody;

        void Awake()
        {
            playerRbody = GetComponent<Rigidbody>();

        }
    private void FixedUpdate()
    {
        playerRbody.AddForce(Physics.gravity * playerRbody.mass);
       // float h = Input.GetAxisRaw("Horizontal");
       // float v = Input.GetAxisRaw("Vertical");
        var jump = Input.GetKeyDown(KeyCode.Space);
        if (!canJump && playerRbody.velocity.y == 0)
        {
            canJump = true;

        }
        if (jump && canJump == true)
        {
            playerRbody.AddForce(new Vector3(0, 6f, 0), ForceMode.Impulse);
            canJump = false;
        }
         if (Input.GetKey(KeyCode.W))
         {
             moveForward(speed);
         }
         else if (Input.GetKey(KeyCode.S))
         {
             moveBack(speed);
         }

         if (Input.GetKey(KeyCode.A))
         {
             moveLeft(speed);
         }
         else if (Input.GetKey(KeyCode.D))
         {
             moveRight(speed);
         }
        transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, transform.localEulerAngles.z);
        //transform.forward = Camera.main.transform.forward;

    }

    }
    private void moveForward(float speed)
    {
        transform.forward = Camera.main.transform.forward;
        // Quaternion newRotation = Quaternion.LookRotation(transf);
        // playerRbody.MoveRotation(newRotation);
        transform.localPosition += transform.forward * speed * Time.deltaTime;
    }

    private void moveBack(float speed)
    {
        transform.forward = -Camera.main.transform.forward;
        transform.localPosition += transform.forward * speed * Time.deltaTime;

    }

    private void moveRight(float speed)
    {
        transform.forward = Camera.main.transform.right;
        transform.localPosition += transform.forward * speed * Time.deltaTime;
    }

    private void moveLeft(float speed)
    {
        transform.forward = -Camera.main.transform.right;
        transform.localPosition += transform.forward * speed * Time.deltaTime;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-08-30 00:55:12

不要那样处理变换位置。

https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

https://docs.unity3d.com/ScriptReference/Rigidbody.html

和Vector3.Up * 6f与新的Vector3(0,6,0)相同;如果你想让你的球员向上移动,但他的旋转方式不同,他不会使用新的Vector3()相对于他的旋转向上移动,使用它的变换来找出up指向的地方。

我也建议不要把播放器从相机上移到右/上/左/等等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39211087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档