Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NightmaresDev · Apr 21, 2017 at 10:20 PM · c#scripting problemplayertransform.positionshortcut

Getting the Player in a Script!

So what I am trying to do is that player = (Person with the tag "Player") Instead of a GameObject!
And this should be done in the Start Method. If you do not understand this it should be like this..

public Transform player;

void Start()

{
player = tag("Player")
}

This is my script if this helps!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyChase : MonoBehaviour {
 
     public float distanceToPlayer;
 
     public Transform player;
     public Animator anim;
 
     public float rotationSpeed = 2f;
 
 
     void Start ()
     {
         anim = GetComponent<Animator>();
         player = tag("Player"); // Doesnt work
     }
 
     void Update ()
     {
         Vector3 direction = player.position - this.transform.position;
         float angle = Vector3.Angle(direction, this.transform.forward);
         if (Vector3.Distance(player.position, this.transform.position) < 15 && angle < 280)
         {
             
             direction.y = 0;
 
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                 Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 
 
             anim.SetBool("IsIdle", false);
             if(direction.magnitude > distanceToPlayer)
             {
                 this.transform.Translate(0, 0, 0.16f);
                 anim.SetBool("IsWalking", true);
                 anim.SetBool("IsAttacking", false);
             }
             else
             {
                 anim.SetBool("IsAttacking", true);
                 anim.SetBool("IsWalking", false);
             }
         }
         else
         {
             anim.SetBool("IsIdle", true);
             anim.SetBool("IsWalking", false);
             anim.SetBool("IsAttacking", false);
         }
     }
 
     
 }
 

Thanks for the Help!

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

2 Replies

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

Answer by Commoble · Apr 21, 2017 at 10:21 PM

GameObject.tag is a member variable, not a function. Do this instead:

 this.player = GameObject.FindWithTag("Player").transform;

You can read the documentation for FindWithTag() here.

Comment
Add comment · Show 1 · 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 NightmaresDev · Apr 22, 2017 at 08:22 AM 0
Share

I wanna Thank you a lot! Because this is currently working perfectly fine!

Thanks!

avatar image
0

Answer by Razputin · Apr 22, 2017 at 01:57 AM

player = GameObject.FindWithTag("Player").transform;

Why do you want your players transform though? Might be easier to just grab the whole thing instead.

Public GameObject player;

player = GameObject.FindWithTag("Player");

Then if you want to call the transform anywhere just call

player.transform;

Comment
Add comment · Show 1 · 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 NightmaresDev · Apr 22, 2017 at 08:23 AM 0
Share

Thank you ^^

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get my Player (which is a square) to move smoothly in Unity 2d? I still need help, even though there are answers. 2 Answers

Player changes scale when i play the game 1 Answer

Two methods are called on repet in my code and I don't know why 1 Answer

how to use right stc on gamepad like Input.mousePosition 0 Answers

Boosting Character Controller Problem 0 Answers


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