- Home /
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;
}
}
}
}
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.
Your answer
Follow this Question
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
Incremental Move and stop 1 Answer
Internal Compiler Error - Lidgren 0 Answers
Particle circle HELP 1 Answer