- Home /
how to make a Ghost and a fly script
Hi. i have a character and i want to make him fly and i want him to have a ghost ability (he can walk throw walls)
Answer by nhftk12 · May 28, 2011 at 09:58 AM
This is the flying code:
var targetDistance;
var target : Transform;
var moveSpeed = 8.0;
var damping = 3.0;
function Update ()
{
targetDistance = Vector3.Distance(target.position, transform.position);
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
var offsetDirection = target.position - transform.position;
offsetDirection.y = 0;
var rotation2 = Quaternion.LookRotation(offsetDirection);
transform.rotation = Quaternion.Slerp(transform.rotation2, rotation, Time.deltaTime * damping);
transform.Translate(offsetDirection.normalized * moveSpeed *Time.deltaTime, Space.World);
}
Attack this to the ghost, it will follow you allways at the same height.
For the ability of going through wall: just don't put any colliders on the ghost. If you want it to collide with something: put all the collders on "Trigger".
If you want it not to look at you but still look to your dirction (disable rotation on the x and z axis) $$anonymous$$ETE this line: "var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
"
how can i make the character moves right and left while fling
If you want the ghost will stay on the same height and go to the side add this line to the end of the code "transform.Translate(Vector3.right moveSpeed Time.deltaTime);" (in this line it will go to the right, you can change the sides by write "left" or "right"), BUT the ghost will go to the side AND forward, it will seems like the ghost is tring to pass you from your side. If you want the ghost will really go to the side so delete the last 5 lines in the code and add the line I gave you now, there is a problem with that because I didn't really tried the code by going to the sides so I don't sure the ghost will keep to go on the same height... (It not me who made that code...)
thanks very much the code is completely ok but the ghost walks throw the sides i don't want the ghost to walk throw any thing
Your answer
Follow this Question
Related Questions
Problems entering in Fly Mode 1 Answer
ObjectSpawner 1 Answer
Move Object a direction with one button and move the object back with the same button? 1 Answer
Another question about the flying enemy 1 Answer
Identifying a particular script 0 Answers