- Home /
How do I get objects position and use it as a target variable in csharp?
Greetings.
I have a script that allows an object to move and rotate to a specific target, via dragging the desired object from the hierarchy onto the target slot in the inspector view. So what I want to do is to set the target without having to drop it on a slot manually, but I can't figure out how to do that.
This is the script:
using UnityEngine;
using System.Collections;
public class MoveToTarget : MonoBehaviour {
public Transform target;
public float speedRotation = 5;
public float movingSpeed = 80;
public float minimalDistance = 2;
public float headSpeedRotation = 5;
private Quaternion initialRotation;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
Walk(target.position);
RotateTo(target.position);
}
public void RotateTo(Vector3 targetPosition)
{
Quaternion destRotation;
Vector3 relativePos;
Vector3 tap;
Vector3 trp;
tap = targetPosition;
trp = transform.position;
tap.y = 0;
trp.y = 0;
relativePos = tap - trp;
destRotation = Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Slerp(transform.rotation,destRotation,speedRotation *Time.deltaTime);
}
public void Walk(Vector3 targetPosition){
Vector3 velocity;
Vector3 moveDirection = transform.TransformDirection(Vector3.forward);
Vector3 delta = targetPosition - transform.position;
if (delta.magnitude > minimalDistance)
{
animation["Walk"].speed = 1.4f;
velocity = moveDirection.normalized * movingSpeed * Time.deltaTime;
animation.CrossFade("Walk");
}
else
{
velocity = Vector3.zero;
animation.CrossFade("Idle");
}
rigidbody.velocity = velocity;
}
}
My question is: How do I set the variable target.position in the update function equal to, let's say an object with the tag "Enemy"?
I'd be glad for any advice.
Answer by aldonaletto · Oct 15, 2011 at 11:57 AM
Usually you set the target variable at Start using FindWithTag:
void Start(){ target = GameObject.FindWithTag("Enemy").transform; }You should not use any Find family instructions inside Update because they are all slow operations, and will impact the frame rate.
Answer by Noah1982 · Oct 15, 2011 at 02:22 PM
Another Question: I'm using the downloadable headlookcontroller script. In it, there's a Vector3 variable called target. That's the variable that refers to the object in worldspace, that the character will look at. Is there any way to call this variable in the script above and make it equal to the target variable that I we have set to GameObject "Enemy"? I tried the following:
First I changed the target variable in the headlookcontroller script from "public Vector3 target;" to "public static Vector3 target;" so I can access it in the MoveToTarget script.
Then in the MoveToTarget script:
void Start() {
target = GameObject.FindWithTag("Enemy").transform;
HeadLookController.target = target.position;
}
This doesn't work. My goal is, to make the headlookcontroller Vector3 target equal to the MoveToTarget target after we set it equal to the object with tag "Enemy", but I have no Idea how to accomplish that... (I tried many combinations, but everything I get is a lot of parse errors!)
Please help! :(
The code above should work - but with a problem: enemies usually don't stay in place, they move around a lot. You can get the enemy position at Start, but when it moves the position saved becomes useless. I don't know how HeadLookController is, but I suppose you can create a Transform variable targetTransf and assign the enemy transform to it, then in Update feed target with targetTransf.position.
Thanks for the advice. The headlookcontroller thingy is a bit too difficult for me right now, other then that everything works smooth, thanks to you.
Have a nice weekend.