- Home /
Rotate GameObject Issue
So I have a gameobject that I currently have locked onto another object. It's setup with the following code:
var objectToFollow;
function Update ()
{
if(gameObject.Find("localPlayer")==null)
{}
else
{
objectToFollow = GameObject.FindWithTag("Player").transform;
transform.position = objectToFollow.TransformDirection(Vector3.forward * 4) + objectToFollow.position;
transform.rotation = objectToFollow.rotation;
//needs to be rotated by 0,-90,90
}
}
So the problem I have, as seen by the comment, is that the object needs to be rotated. I've tried a few methods of doing this but they don't work for a specific reason. I need the object to stay in rotation relation to the player. It currently does this with the code, but it's facing the wrong way. I can set it up so that on start of gameplay it's facing the right way, but if I turn it stays pointing the same direction in the world instead of continuing to point at me. This is because the object is a child of my avatar, as it needs to remain.
Any ideas? Thanks!
Answer by ckfinite · Jun 21, 2011 at 09:04 PM
Didn't I anwser this originally? Anyway, try this:
var objectToFollow;
function Update ()
{
if(gameObject.Find("localPlayer")==null)
{}
else
{
objectToFollow = GameObject.FindWithTag("Player").transform;
transform.position = objectToFollow.TransformDirection(Vector3.forward * 4) + objectToFollow.position;
transform.rotation = objectToFollow.rotation * Quaternion.Euler(0,-90,90);
}
}
Yup you did, different application atm.
But question about this. Is there any reason this script would cause On$$anonymous$$ouseDown() to not work? I have this code:
void Start () {renderer.material.color = Color.blue; } void On$$anonymous$$ouseDown() {renderer.material.color = Color.green;}
attached to a child of the object with the code you've scripted in the answer. The object renders blue but not green on click.
Yeah it works great. I forgot that I deleted my mesh collider to solve an earlier issue so that's why the function didn't work -_- good to go now, thanks!
Your answer
Follow this Question
Related Questions
Rotation is jumpy 2 Answers
How get the rotation of an GameObject? 3 Answers
Rotate around a locally tilted axis?, 1 Answer
How do I rotate an object a certain number of degrees? 2 Answers