当玩家击中墙壁时,它会开始弹跳,如何让玩家正常移动

时间:2016-10-06 12:44:25

标签: unity3d gameobject

using UnityEngine;
using System.Collections;

public class Accelerometer : MonoBehaviour {

public Vector3 positionplayer;

// Update is called once per frame
void Update () 
{
     transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
     transform.Rotate (0,0,0);
}

}

我已经将这个脚本附加到玩家,每件事情都运转良好但是当它碰到障碍物时它开始弹跳......但它不会在地面上反弹(只有当墙被击中时)

我使用飞机和使用立方体的四面墙做了地面

对于玩家添加了一个对撞机,刚体并将刚体设置为重力与质量1

对于墙壁,我添加了箱式对撞机,只是一个刚性的

这在我的新代码中

using UnityEngine; 
using System.Collections;

public class Accelerometer : MonoBehaviour {

public Vector3 positionplayer;
Rigidbody b;
public float speed = 10.0f;
private Quaternion localRotation;

// Use this for initialization
void Start () {

 localRotation = transform.rotation;

 }

// Update is called once per frame
void Update () 
{

float curSpeed = Time.deltaTime * speed;

//transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);

transform.position += (transform.right * Input.acceleration.x) * curSpeed;

transform.position += (transform.forward * -Input.acceleration.z) * curSpeed;

//transform.Rotate (Input.acceleration.x,0,0);



localRotation.z += -Input.acceleration.z * curSpeed;
localRotation.z += Input.acceleration.z * curSpeed;

//transform.rotation = localRotation;

print (transform.position);
}
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下几种方法:

  • 从墙上移除rigidbody
  • 使用rigidBody组件而不是transform.Translate移动您的播放器。

以下是Unity Tutorial的示例代码:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    public float speed;
    Rigidbody rigidbody;

    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rigidbody.velocity = movement * speed;
    }
}