Jump to content
  • Advertisement
Sign in to follow this  
TagRawnix

Unity unity : rotate player to face mouse cursor

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the following script in place but I can seem to get the character to face the mouse cursor. I removed my tests to keep it clean

Now, w, a, s, d should move the character relative to the direction your pressing = so this would be attached to the pelvis object of the model

The top half of the player should face where the mouse pointer is = attached to main character object.

This must be possible??



using UnityEngine;
using System.Collections;


public class Movement : MonoBehaviour
{
public float moveSpeed = 5;
public float runMultiplier = 2;
public float strafeSpeed = 2.5f;
public float rotateSpeed = 250;

private Transform _myTransform;
private CharacterController _controller;


//Called before the script starts
public void Awake()
{
_myTransform = transform;
_controller = GetComponent<CharacterController>();
}

// Use this for initialization
void Start()
{
animation.wrapMode = WrapMode.Loop;
animation["idle"].wrapMode = WrapMode.Loop;
}


void Update()
{
if (!_controller.isGrounded)
{
_controller.Move(Vector3.down * Time.deltaTime);
}

Turn();
Walk();
Strafe();
}

private void Turn()
{
if (Mathf.Abs(Input.GetAxis("Rotate Player")) > 0)
{
// Debug.Log("Rotate: " + Input.GetAxis("Rotate Player"));
_myTransform.Rotate(0, Input.GetAxis("Rotate Player") * Time.deltaTime * rotateSpeed, 0);

// this is where I had a OnGUI() method for the target image
}
}

private void Walk()
{
if (Mathf.Abs(Input.GetAxis("Move Forward")) > 0)
{
// Debug.Log("Move Forward: " + Input.GetAxis("Move Forward"));
if (Input.GetButton("Run"))
{
animation.CrossFade("run");
_controller.SimpleMove(_myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Move Forward") * moveSpeed * runMultiplier);
}
else
{
animation.CrossFade("walk");
_controller.SimpleMove(_myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Move Forward") * moveSpeed);
}
}
else
{
animation.CrossFade("idle");
}
}

private void Strafe()
{
if (Mathf.Abs(Input.GetAxis("Strafe")) > 0)
{
// Debug.Log("Strafe: " + Input.GetAxis("Strafe"));
animation.CrossFade("side");
_controller.SimpleMove(_myTransform.TransformDirection(Vector3.right) * Input.GetAxis("Strafe") * strafeSpeed);
}
}



thanks for your time

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!