Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noah1982 · Oct 15, 2011 at 11:50 AM · positionobject

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Noah1982 · Oct 15, 2011 at 02:22 PM

Thanks alot aldonaletto! That works!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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! :(

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Oct 15, 2011 at 02:33 PM 0
Share

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.

avatar image Noah1982 · Oct 15, 2011 at 04:37 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D get touch input 1 Answer

how to set rotation / position of an object on trigger? 2 Answers

Returning position of game object in c# 2 Answers

Rotate a thrown spear to Mouse Position (2d top down game) 0 Answers

How do I position a GUI Label over an object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges