- Home /
Rotate a gameobject(player)on the y axis towards mouse position in c#
been looking for a way to do this but havent found it in c# pleas god some one help us.
this is what we have so far for are character
using UnityEngine; using System.Collections;
public class carl : MonoBehaviour { public GameObject ProjectilePrefab;
public float PlayerSpeed;
// Update is called once per frame void Update () {
// Amout to move
float inputX = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
float inputY = Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime;
// Move the Player
transform.Translate(Vector3.forward * inputX) ;
transform.Translate(Vector3.left * inputY) ;
if (Input.GetKey("space"))
{
//Fire projectile
Instantiate(ProjectilePrefab, transform.position, Quaternion.identity);
}
}
}
Im trying the exact same thing here - did this ever get sorted?
Answer by IJM · Oct 04, 2010 at 08:33 PM
You can do this:
Vector3 MouseWorldPosition = MainCamera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
transform.LookAt(MouseWorldPosition);
transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));
Call this from your player script.
cool thanks but now i get an error '$$anonymous$$ainCamera' does not exist in the current context
any ideas
got it working but the player spins out of control and isnt looking at the mouse pointer position.
yeah we added it in and the player immediately turned 90 deg and would run around and rotate, just not in the direction of the mouse position
You are doin something wrong. Can you put the whole player code down, with the implementation of this? (as a answe)
Answer by bryan · Oct 05, 2010 at 04:19 PM
using UnityEngine; using System.Collections;
public class carl : MonoBehaviour { public GameObject ProjectilePrefab; public GameObject MainCamera;
public float PlayerSpeed; public float QuaternionSpeed;
// Update is called once per frame void Update () {
Vector3 MouseWorldPosition = MainCamera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
transform.LookAt(MouseWorldPosition);
transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));
// Amout to move
float inputX = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
float inputY = Input.GetAxis("Vertical") * PlayerSpeed * Time.deltaTime;
// Move the Player
transform.Translate(Vector3.forward * inputX) ;
transform.Translate(Vector3.left * inputY) ;
if (Input.GetKey("space"))
{
//Fire projectile
Instantiate(ProjectilePrefab, transform.position,Quaternion.identity);
}
}
} there it is, he moves, and rotates kinda randomly, but does not rotate to look at mouse postition
it lost the top part here using UnityEngine; using System.Collections;
public class carl : $$anonymous$$onoBehaviour { public GameObject ProjectilePrefab; public GameObject $$anonymous$$ainCamera;
public float PlayerSpeed; public float QuaternionSpeed;
You need something like this in your Start() function: $$anonymous$$ainCamera = GameObject.Find("CameraName"); You need to know what is the name of your camera in Editor. (CameraName)
I think it's Camera.mainCamera
, not $$anonymous$$ainCamera.camera
. :)
Your answer
Follow this Question
Related Questions
2D rotate object towards mouse? 1 Answer
Why Rotate player toward mouse direction keeps increasing rotation speed? 1 Answer
How to make a 2D character points his gun to the mouse position? 2 Answers
C# Rotate GameObject at Other Point other than Center 3 Answers
Horizontal gravity on One gameobject 2 Answers