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

}

i have attached this script to the player,every thing is working fine but when it hits an obstacle it starts bouncing..but it doesn't bounce on the ground(only when the wall is hit)

i have made the ground using plane and four walls using cube

For player have added a collider, rigid body and set rigidbody to gravity with mass 1

for the walls i have added box collider and just a rigidbody

this in my new code

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
This may not be relevant to your situation but if you don't want your transform to rotate you should check the settings in the rigidbody so that those values don't change. It separates it by x/y/z coordinates so you can be specific with it.Ethan The Brave

1 Answers

1
votes

Here are few things you can try:

  • Remove rigidbody from walls
  • move your player using rigidBody component instead of transform.Translate.

Here is a sample code from 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;
    }
}