x


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);


    }



}

}

more ▼

asked Oct 04 '10 at 08:19 PM

bryan gravatar image

bryan
1 3 5 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Oct 04 '10 at 08:33 PM

IJM gravatar image

IJM
1.4k 2 5 19

cool thanks but now i get an error 'MainCamera' does not exist in the current context

any ideas

Oct 04 '10 at 11:57 PM bryan

got it working but the player spins out of control and isnt looking at the mouse pointer position.

Oct 05 '10 at 12:05 AM bryan

Did you call this from your players script?

Oct 05 '10 at 12:18 AM IJM

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

Oct 05 '10 at 01:13 AM bryan

You are doin something wrong. Can you put the whole player code down, with the implementation of this? (as a answe)

Oct 05 '10 at 12:13 PM IJM
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Oct 05 '10 at 04:19 PM

bryan gravatar image

bryan
1 3 5 7

it lost the top part here using UnityEngine; using System.Collections;

public class carl : MonoBehaviour { public GameObject ProjectilePrefab; public GameObject MainCamera; public float PlayerSpeed; public float QuaternionSpeed;

Oct 05 '10 at 04:20 PM bryan

You need something like this in your Start() function: MainCamera = GameObject.Find("CameraName"); You need to know what is the name of your camera in Editor. (CameraName)

Oct 06 '10 at 05:36 PM IJM
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2084
x1035
x983
x723
x132

asked: Oct 04 '10 at 08:19 PM

Seen: 4583 times

Last Updated: Oct 04 '10 at 08:19 PM