So I have some pigs running in a pen.
I’m trying to make them change directions when they hit the side so they don’t keep running into the wall forever.
Problem is, they keep wanting to go straight along the global forward when they hit a wall.
Here’s my horrible code:
#pragma strict
//Place on Pig Object
var direction : Vector3 = Vector3.forward;
var speed = 5;
var godir : Vector3;
function Start () {
}
function Update ()
{
var hit : RaycastHit;
godir = transform.rotation * direction;
if (Physics.Raycast (transform.position, Vector3.forward, hit, 1.0)) {
transform.localEulerAngles = Vector3.Reflect(godir, hit.normal);
}
}
function FixedUpdate () {
rigidbody.velocity = transform.rotation * direction * speed;
}
Thanks, but the problem lies in the update function.
If I delete whats in the fixed update and apply motion with a constant force, it still doesn’t work.
I also have the pig’s x and z rotation frozen.
Vector3.forward = Shorthand for writing Vector3(0, 0, 1)
It’s not affected by anything.
I may misunderstand how you want to move the object but this info may give you a clue as to what is happening.
Vecort3.back = Shorthand for writing Vector3(0, 0, -1)
Vecort3.up = Shorthand for writing Vector3(0, 1, 0)
Vecort3.down = Shorthand for writing Vector3(0, -1, 0)
Vecort3.right = Shorthand for writing Vector3(1, 0 ,0)
Vecort3.left = Shorthand for writing Vector3(-1, 0)