- Home /
Independent animal character interacts with 1st Person Controller
In a 1st Person Game I want to let interact an independent animal with the 1st Person controller.
If the animal is in the Back of the 1st Person, it will run to the 1st P. and steal points. If the 1st P. turns and faces the animal it will run away.
I am grateful for solution strategies.
Answer by aldonaletto · Nov 26, 2011 at 01:26 AM
That's the basic idea: get a vector from the animal to the fpc (toFpcDir) and check the angle between -toFpcDir (a vector from the fpc to the animal) and the fpc forward direction; if it's less than some narrow angle (15, in the pseudo code below) the fpc is looking in the animal direction, thus make it beat the road; if greater than this angle, continue moving in the fpc direction until a minimum distance is reached, when you could do the attack and apply damage - something like this (animal script):
var fpc: Transform; // drag the First Person Character here
function Update(){ var toFpcDir = fpc.position - transform.position; // animal->fpc direction var angle = Vector3.Angle(-toFpcDir, fpc.forward); // check the angle if (angle < 15){ // FPC is facing the animal: // run away or go back } else { // FPC is looking to other direction: // continue and attack } } The actual movement of the animal will depend on which kind of object it is. Usually live creatures are based on a CharacterController, thus you should move it with Move or SimpleMove, taking advantage of the calculated toFpcDir vector. You must also decide what exactly the animal will do when the fpc look at it: go back slowly, or turn 180 degrees and run, or a mix of the two things etc.
That's exactly what I was looking for!
Thank you very much for the clear explanation an the simple and useful code snippet.
@maspi, if this answer solved your problem, please click its accept button (the "check mark" button below the voting thumbs) - this may help others to find the solution for similar problems.
Your answer
Follow this Question
Related Questions
first person controller with interaction 2 Answers
Hide cursor and stop interaction 1 Answer
VR UI interaction with ingame hands 1 Answer
Navigate Without Focus? 0 Answers
XR Interaction Toolkit: most correct way of dealing with multiple interactors 2 Answers