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 /
  • Help Room /
avatar image
0
Question by KerMatt · Nov 30, 2016 at 03:31 PM · 2draycast2d gameraycasthit2d

Making a GameObject follow the direction of a RayCast

Right so i have an Enemy Prefab with a hierarchy something like this

Enemy | -Rotate Point |-Gun |-FirePoint

The aim is to have the gun point at the player and follow it as the player moves. The Rotate point is there so the Gun GameObject rotates around a certain point to making it look more natural.

I currently have the script attached to the RotatePoint GameObject in the heirachy :

     void Update()
     {
         Vector3 difference = new Vector3(GameObject.FindGameObjectWithTag("Plane").transform.position.x, GameObject.FindGameObjectWithTag("Plane").transform.position.y, 0);
         difference.Normalize();
 
         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);
     }

The gun currently does sort of track the player but is very slow in doing so and occasionally enemies will spawn firing way off from the player see screenshot. alt text

Just trying to find a method that tracks the player smoother and more accurately than my current method. Any help would be greatly appreciated.

untitled.png (247.1 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ComradeVanti · Nov 30, 2016 at 06:01 PM

Try this:

 void Update ()
     {
         Vector3 playerPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
         transform.LookAt (playerPos);
     }

This will always point the gun at the player. If you want the gun to follow the player with a set speed but not instantly try this:

 void Update(){
 
         float speed = 5; //Some Speed in anglesPerSecond
         Vector3 playerPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
         Vector3 difference = playerPos - transform.position;
         Quaternion goalRot = Quaternion.LookRotation (difference);
         transform.rotation = Quaternion.RotateTowards (transform.rotation, goalRot, speed * Time.deltaTime); //This will rotate the gun 5° per second towards the player
     }

Good luck :)

Comment
Add comment · Show 3 · 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 KerMatt · Nov 30, 2016 at 06:39 PM 0
Share

I tried the first one but the the Gun just disappears?

avatar image KerMatt KerMatt · Nov 30, 2016 at 06:49 PM 0
Share

It seems rather than disappear, the gun actually just got squished and placed behind the planealt text

plane.png (4.0 kB)
avatar image ComradeVanti KerMatt · Dec 01, 2016 at 05:53 PM 0
Share

Is this something you can fix on your own?

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

132 People are following this question.

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

Related Questions

2d object moves only a little then stops 0 Answers

Hookshot in Unity 2D not working,Hookshot 2D not working 0 Answers

Basic AI avoidance in 2D 1 Answer

2D raycast not working 0 Answers

Select from multiple character to move 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