- Home /
RayCast Hit and look at target different objects
Hi guys i have to object ; first one is Player; that contains; clicktomove script; second one is Enemy; that contains; Aifollow script; here is the scripts; clicktomove script;
var CubeInstance : GameObject;
var smooth:int;
private var targetPosition:Vector3;
var speed = 60;
var rotationSpeed = 5;
var target : Transform;
var myTransform : Transform;
function Start () {
CubeInstance = GameObject.Find("Cube");
}
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
smooth=1;
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
var dir:Vector3 = targetPosition - transform.position;
var dist:float = dir.magnitude;
var move:float = speed * Time.deltaTime;
if(dist > move){
transform.position += dir.normalized * move;
} else {
transform.position = targetPosition;
}
transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
}
here is the second script aifollow;
var target : Transform;
var moveSpeed = 200;
var rotationSpeed = 5;
var myTransform : Transform;
var playerdistance : int;
var player : GameObject;
function Awake()
{
myTransform = transform;
}
function Start()
{
player = GameObject.Find("Player");
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if(playerdistance >= 1000) {
moveSpeed = 0;
}
if(playerdistance < 1000) {
moveSpeed = 200;
}
playerdistance = Vector3.Distance(player.transform.position, transform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
clicktomove script::::: with the click to move script i can move the Player with mouse button 0,,, Player moves x axis aifollow script::::: with the aifollow script Enemy looks at the Player and it follow him x axis.
now i want to do something like this : if i Click mousebutton 0 the collider of the Enemy or click the Enemy,,, Player will look at the Enemy but player already can move. and if i click mouse button 0 out of the collider of enemy Player will stop to look at the enemy.
now who can writes me the codes or help me pleaseee i need this thank you. :)
Your answer
Follow this Question
Related Questions
Is it the same javascript 1 Answer
how to delay a object from moving 1 Answer
How do you make a trig object? 0 Answers
Referencing subclass in Unity JS 2 Answers
I need instantiated game objects to move once they're instantiated 3 Answers