Pichi Wuana
Pichi Wuana

Reputation: 752

My object begins to spin when collides with the wall

I have in Unity a box that is followed by a camera on a plane. I'm trying to handle the collisions between the box and different objects. When it collides with different things it spins, jumps and happen weird things. I uploaded to YouTube a video to show the problem. The video.

I created an empty that has the camera and the box. This empty has rigidbody of mass 1.

The empty has a script component:

using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

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

    }
    void OnCollisionEnter(Collision collision)
    {
        Debug.Log ("Entered OnCollisionEnter function");
        if (collision.gameObject.name == "Wall") {
            GetComponent<Rigidbody>().velocity = Vector3.zero;
            Debug.Log ("Inside if statement");
        }

    }
}

As you can see I tried to handle the collision writing a code that stops the cube of moving.


Additional information that could help you guys:

The box

It has a box collider. Script:

using UnityEngine;
using System.Collections;

public class MoveCharacter : MonoBehaviour {

    public float deltaMovement = 10f;

    // Use this for initialization
    void Start ()
    {
    }

    // Update is called once per frame
    void Update ()
    {
        Moving();
    }


    void Moving()
    {
        //Moves the character to where it needs.
        if (Input.GetKey (KeyCode.A)) {
            transform.Translate (new Vector3 (-deltaMovement, 0f, 0f) * Time.deltaTime);
        } else if (Input.GetKey (KeyCode.D)){
            transform.Translate (new Vector3 (deltaMovement, 0f, 0f) * Time.deltaTime);
        }
        float yRotation = Camera.main.transform.eulerAngles.y;
        float movementX = Mathf.Sin ((yRotation * Mathf.PI) / 180) * deltaMovement;
        float movementZ = Mathf.Cos ((yRotation * Mathf.PI) / 180) * deltaMovement;

        if (Input.GetKey (KeyCode.W)) {
            transform.Translate (new Vector3 (movementX, 0f, movementZ) * Time.deltaTime, Space.World);
        } else if (Input.GetKey (KeyCode.S)){
            transform.Translate (new Vector3 (-movementX, 0f, -movementZ) * Time.deltaTime, Space.World);
        }
    }
}

The wall

It is a plane with mesh collider and with or without rigidbody didn't make a difference, same problem...


Any help please?

Upvotes: 1

Views: 360

Answers (1)

Graeme
Graeme

Reputation: 1698

If you are using physics you shouldn't move an object my changing it's translation, you should move it by applying forces to the object, or at least adding a velocity to it. This will allow the physics engine to correctly calculate the reactions with other rigid bodies.

If you move an object my adjusting it's translation then when a collision occurs it will be as though the object has materialised into the other object to the engine as it will be moving with no velocity etc, and you will get weird behaviour.

Upvotes: 2

Related Questions