• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by sirop · Dec 26, 2014 at 10:45 PM · c#errormovementrotations

How to rotate an object and make it to follow another gameobject?

I have a boy in my 2D game. He needs to take a separately standing gun and the gun has to follow him during the game untill he takes another one. The problem is that the gun is standing under 90'angle but when the boy takes it I want it to be under other angle. I tried everything and do not know how to make it. Please help.I found ideas on forums but they do not work for some reason. How do you make it?

 using UnityEngine;
 using System.Collections;
 
 public class gun : MonoBehaviour {
     public GameObject Bullet;
     private Transform myTransform;
     public bool FollowBoy = false;
     public float smooth;
     public GameObject Boy;
     private Vector2 perehodgun;
 
     void Awake ()
     {
         perehodgun = transform.position;
         Boy = GameObject.Find("Boy");
 
     }
     void Start () 
     {
         myTransform = transform;
             }
     void Guntoboy ()
     {
         Vector2 perehodgun = Boy.transform.position;
 
         transform.position = Vector2.Lerp(transform.position, perehodgun, smooth * Time.deltaTime);
 
     }
 
     void Update () 
     {
         if (FollowBoy)
         {
 
         Guntoboy ();
             if (Input.GetKeyDown(KeyCode.G))
             {
                 Vector2 position5 = new Vector2(myTransform.position.x, myTransform.position.y);
                 Instantiate (Bullet, position5,Quaternion.identity);
             }
         }
 
         if (GameObject.Find("Boy").GetComponent<PlayerControlerScript>().takeGun) //will check if true
         {            
             if (Input.GetKeyDown(KeyCode.F))
             {
             transform.Rotate(new Vector3(0,0,50)); //it is not working and shouldnot be here`
             FollowBoy = true;
 
             }
 
         
         }
     }
 
 }




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 sirop · Dec 28, 2014 at 06:47 AM

 public Vector3 temp;
 temp.z = 0.0f;
 transform.rotation = Quaternion.Euler(temp);

I found the answer. It is so easy but hard for a beginner like me to figure it out.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Need help calling a script to another keep getting errors 1 Answer

Following the wiki.unity scripting tutorial... 1 Answer

Particle circle HELP 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