- Home /
Character rigidbody collisions
Hi,
I'm fairly sure I'm having a complete brain-freeze about this, but I'm struggling to get my head around rigidbodies and general physics interactions.
I'm currently moving my player character around with the code below, but I can't seem to get the characters to collide with any objects.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 10f;
private Vector3 lookTarget;
void FixedUpdate()
{
// Forward and sideways movement
float transH = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
float transV = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
transform.Translate(transH, 0, transV, Space.World);
// Mouselook
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
}
There is a rigidbody attached to the player, along with a capsule collider (the player is currently just a capsule primitive anyway) and there is a box collider on the basic wall I've been trying this on.
My understanding was that 'Use gravity' should be disabled if the object is being moved by its transform, but my character passes through any object unless this is ticked - I've attempted various combinations ('Is Kinematic' ticked and unticked, a rigidbody attached to the wall, etc.), but it's got to the point where I've got completely lost.
Any help would be appreciated!
Sounds like you have "trigger" ticked in the inspector that would prevent collisions my friend.
From what i know if you use Transform.Translate or change the transform.position on a gameobject it will not react to collisions. If you have a rigidbody attached you should use Rigidbody.AddForce. Google for it and you should find several rigidbody movement scripts.
Answer by DESTRUKTORR · May 13, 2013 at 08:44 PM
If you manually change positions via the transform, you will be able to pass through objects. Telling it to position itself in the middle of another collider will trigger a collision, but will not force itself out from the other collider. You will need to use either rigidbody.AddForce()
or rigidbody.velocity
to govern speed or direction of movement, if you want collisions to be included in the physics calculations.
Hi, sorry for the late reply!
$$anonymous$$y problem with using AddForce() (or AddRelativeForce()) is that there doesn't seem to be any fine control over the movement. Also, as this is a player movement script, it is being called every FixedUpdate(), so the player velocity is increasing over time.
I know I can clamp the velocity, but I still have issues with the character slowing to a stop after the input is released.
if(!Input.GetButton ("WhateverYouChose"))
if(yourDesiredObject.rigidbody.velocity.sqr$$anonymous$$agnitude > 0.0001f)
yourDesiredObject.rigidbody.velocity *= 0.95f;// Any literal float between 0 and 1 (not including 1.0f) will work, here.
else
yourDesiredObject.rigidbody.velocity = Vector3.zero;
All AddForce and AddRelativeForce do is add to the unit's immediate velocity, based on its mass.
Another option would be to change the physic material on your object's collider to one with a greater amount of friction on it.