- Home /
Inheriting orientation from collision
Hi there,
I have my player who is moving in a straight line. When he hits a "waypoint" I want him to rotate to the same orientation as the waypoint and continue to move forward in his new orientation until he hits another waypoint.
I have my player moving and writing to the log when he hits a waypoint, but I don't know how to get the orientation information from the waypoint and then apply it to the player.
When I set out to do it I thought it would be simple, but it's not... :(
I'm very new to Unity and scripting, so I would really appreciate some help.
Answer by Waz · Aug 08, 2011 at 09:14 AM
Assuming the direction of waypoint2 is towards waypoint3, you can use:
player.transform.rotation = Quaternion.LookRotation(waypoint3-waypoint2);
Or even just player.transform.LookAt(waypoint3).
I have set the waypoint as a target variable. Is there a way to change that target to the next target once I have hit one?
e.g. I set the first target to always be "waypoint0". When the player object hits "waypoint0" I want it to change (its) 'target' to "waypoint1"
I was thinking that I could have another variable for the waypoint number, and increment it each time a waypoint is hit. The target would then be ("waypoint" + waypointNumber) sort of thing.
I just don't know how to translate this into working code...
Answer by Mythix · Aug 08, 2011 at 02:27 PM
OK, so here's what I came up with to get it to work...
private var target : Transform;
private var targetWaypoint : String;
private var wayPointNumber : int;
var speed : int = 1;
wayPointNumber = 0;
function Update () {
transform.Translate(0,0,speed*Time.deltaTime);
targetWaypoint = ("waypoint" + wayPointNumber);
target = GameObject.Find(targetWaypoint).transform;
transform.LookAt (target);
}
function OnTriggerEnter(waypointCollision:Collider){
if(waypointCollision.gameObject.tag == "Waypoint"){
wayPointNumber = wayPointNumber + 1;
}
}
This does what I need, but with LookAt it's snapping from each target to the next. I would love to dampen the LookAt. I have looked at the original script for the camera, but I'm not sure how to work the damping for that into what I have there. I've tried but couldn't get a result...
I just want it to rotate a bit smoother on the Y.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Event for orientation change done? 1 Answer
How to get my rigidbody character to orient rotation to a game object 0 Answers
How to fix the rotation and Orientation in an object as a Children ? 1 Answer
How to keep character facing 3/4 front when moving right and left in 2.5D game 0 Answers