当玩家撞到墙上时它开始弹跳,如何让玩家正确移动

when player hits on a wall it starts bouncing, how to make player move properly

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);
}
}

您可以尝试以下几项操作:

  • 从墙上移除 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;
    }
}