I’m having a little problem making my main character in my project run along walls. What he’s meant to do when hitting a wall at an acute angle (<15degrees or so) is align in such a way as to be running along the wall. The code I’ve been trying to butcher into something that works is getting quite messy:
function OnCollisionEnter(collision : Collision) {
var contact = collision.contacts[0];
var incidence = Vector3.Angle(rigidbody.velocity, contact.normal);
if ((incidence > 75)(state == 2)) { // hit a wall at angle
transform.rotation = Quaternion.FromToRotation (transform.up, contact.normal);
rigidbody.velocity.y = 0;
state = 0;
gameObject.Find("firedparticles").particleEmitter.emit = false;
Physics.gravity = Vector3(0,-8,0);
print(incidence);
}
}
the print is showing me that I’m getting into the if statement at least, and rigidbody.velocity.y is being killed (I think maybe I should actually be rotating rigidbody.velocity? How would I even do that? vector3 doesn’t seem to have a rotate function). The problem I’m having is that the FromtoRotation doesn’t seem to work. The transform maintains its upright position and as a result when velocity.y is set to 0 the object simply falls down (maintaining x and y, of course).
Not 100% sure but this sticks out to me: transform.rotation = Quaternion.FromToRotation (transform.up, contact.normal);
Thats only rotating the Y axis (turning the charactor left or right). When I think your wanting the charactor to keep its feet on the wall which is X and Z axis. Not able to check this atm but I think thats why its not working.
Thinks for the reply. I’ve been combing the internets and wracking my brain over this since I posted this morning and still coming up with nothing. The trig I’ve been looking at has been giving me a serious headache (I’m normally fine with trig).
The weird thing is that if I have a smooth curved surface leading up to a wall the character can run up it just fine, rotating to the surface contours and giving the behavior I want, but jumping at the wall refuses to work.
If it helps, I’ll make a build and upload for people to pick at. I hate having to ask for answers but I really am stuck with this one.
I’m about ready to give up on this. I think my best option at this point is to try and dynamically change the physic material to prevent bouncing, which will fix the velocity vector. I can then set transform.forward to rigidbody.velocity. that doesn’t fix rotating the character’s facing but it gets the momentum sorted and maybe I can find another way around the problem from there.
following on from my previous post: how would I go about changing the bounciness value of my PhysicMaterial in a script? collider.bounciness doesn’t work, nor does collider..bounciness
any help?
[edit]
ok, I managed to change the material but it’s still bouncing on the old material values. Will I need to cover my character in a trigger or something to change the material before the collision rather than changing it oncollisionenter?
function checkahead () {
var ahead : RaycastHit;
if (Physics.Raycast(transform.position, rigidbody.velocity.normalized, ahead, 13)) {
var incidence = Vector3.Angle(rigidbody.velocity, ahead.normal);
incidence = 90-incidence;
if (incidence > 25) {
collider.material.bounciness = 0.4;
}else{
transform.rotation = Quaternion.FromToRotation (Vector3.up, ahead.normal);
state = 0;
gameObject.Find("firedparticles").particleEmitter.emit = false;
Physics.gravity = Vector3(0,-8,0);
collider.material.bounciness = 0.0;
}
}
}
no code in the collision event. the distance of the ray is such that there is a very small window between incidence and the rayhit (0.5 distance). Hopfully having rays fire every fixedupdate won’t slow things down too much.