How to rotate the player when moving your mouse?
up vote
0
down vote
favorite
What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.
Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.
This is my Code:
#pragma strict
var spinx : int = 0;
var spiny : int = 0;
var spinz : int = 0;
function Update () {
transform.Rotate(spinx, spiny, spinz);
}
unity3d rotation unityscript
bumped to the homepage by Community♦ 15 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
0
down vote
favorite
What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.
Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.
This is my Code:
#pragma strict
var spinx : int = 0;
var spiny : int = 0;
var spinz : int = 0;
function Update () {
transform.Rotate(spinx, spiny, spinz);
}
unity3d rotation unityscript
bumped to the homepage by Community♦ 15 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.
Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.
This is my Code:
#pragma strict
var spinx : int = 0;
var spiny : int = 0;
var spinz : int = 0;
function Update () {
transform.Rotate(spinx, spiny, spinz);
}
unity3d rotation unityscript
What I mean in my question is that how to make your player rotate automatically when I move my mouse left for example and my whole characters body will rotate and limit its rotation back to a 2D view.
Similar to the game "Rochard" if you guys know it. But I'm having trouble how to figure it out.
This is my Code:
#pragma strict
var spinx : int = 0;
var spiny : int = 0;
var spinz : int = 0;
function Update () {
transform.Rotate(spinx, spiny, spinz);
}
unity3d rotation unityscript
unity3d rotation unityscript
edited Oct 24 '14 at 12:45
LearnCocos2D
58.8k19114209
58.8k19114209
asked Oct 24 '14 at 11:03
Johndii1491
117
117
bumped to the homepage by Community♦ 15 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 15 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
this script should rotate your object acccording to mouse position
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
add a comment |
up vote
0
down vote
You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.
add a comment |
up vote
0
down vote
The following script should rotate your object according to mouse position,
using UnityEngine;
using System.Collections;
public class RotateClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
add a comment |
up vote
0
down vote
First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.
private Vector3 MousePositionViewport = Vector3.zero;
private Quaternion DesiredRotation = new Quaternion();
private float RotationSpeed = 15;
void Update () {
MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
if (MousePositionViewport.x >= 0.6f) {
DesiredRotation = Quaternion.Euler (0, 90, 0);
} else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
DesiredRotation = Quaternion.Euler (0, 270, 0);
}else {
DesiredRotation = Quaternion.Euler (0, 180, 0);
}
transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
}
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
this script should rotate your object acccording to mouse position
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
add a comment |
up vote
0
down vote
this script should rotate your object acccording to mouse position
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
this script should rotate your object acccording to mouse position
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
this script should rotate your object acccording to mouse position
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
answered Oct 24 '14 at 13:08
Milad Qasemi
2,2132816
2,2132816
add a comment |
add a comment |
up vote
0
down vote
You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.
add a comment |
up vote
0
down vote
You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.
add a comment |
up vote
0
down vote
up vote
0
down vote
You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.
You could import the standard player controls package from unity itself and edit the first person controller to your likings. This is what I tend to do when I want a control in my game that can do what you desire.
answered Oct 26 '14 at 12:36
Jordi Knol
315
315
add a comment |
add a comment |
up vote
0
down vote
The following script should rotate your object according to mouse position,
using UnityEngine;
using System.Collections;
public class RotateClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
add a comment |
up vote
0
down vote
The following script should rotate your object according to mouse position,
using UnityEngine;
using System.Collections;
public class RotateClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
The following script should rotate your object according to mouse position,
using UnityEngine;
using System.Collections;
public class RotateClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
The following script should rotate your object according to mouse position,
using UnityEngine;
using System.Collections;
public class RotateClass : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void Update() {
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
}
}
answered Aug 19 at 11:50
Codemaker
637312
637312
add a comment |
add a comment |
up vote
0
down vote
First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.
private Vector3 MousePositionViewport = Vector3.zero;
private Quaternion DesiredRotation = new Quaternion();
private float RotationSpeed = 15;
void Update () {
MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
if (MousePositionViewport.x >= 0.6f) {
DesiredRotation = Quaternion.Euler (0, 90, 0);
} else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
DesiredRotation = Quaternion.Euler (0, 270, 0);
}else {
DesiredRotation = Quaternion.Euler (0, 180, 0);
}
transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
}
add a comment |
up vote
0
down vote
First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.
private Vector3 MousePositionViewport = Vector3.zero;
private Quaternion DesiredRotation = new Quaternion();
private float RotationSpeed = 15;
void Update () {
MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
if (MousePositionViewport.x >= 0.6f) {
DesiredRotation = Quaternion.Euler (0, 90, 0);
} else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
DesiredRotation = Quaternion.Euler (0, 270, 0);
}else {
DesiredRotation = Quaternion.Euler (0, 180, 0);
}
transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
}
add a comment |
up vote
0
down vote
up vote
0
down vote
First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.
private Vector3 MousePositionViewport = Vector3.zero;
private Quaternion DesiredRotation = new Quaternion();
private float RotationSpeed = 15;
void Update () {
MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
if (MousePositionViewport.x >= 0.6f) {
DesiredRotation = Quaternion.Euler (0, 90, 0);
} else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
DesiredRotation = Quaternion.Euler (0, 270, 0);
}else {
DesiredRotation = Quaternion.Euler (0, 180, 0);
}
transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
}
First, I am really sorry to write it in C#. But I hope it can help you. This script is attached to player gameobject.
private Vector3 MousePositionViewport = Vector3.zero;
private Quaternion DesiredRotation = new Quaternion();
private float RotationSpeed = 15;
void Update () {
MousePositionViewport = Camera.main.ScreenToViewportPoint (Input.mousePosition);
if (MousePositionViewport.x >= 0.6f) {
DesiredRotation = Quaternion.Euler (0, 90, 0);
} else if(MousePositionViewport.x < 0.6f && MousePositionViewport.x > 0.4f){
DesiredRotation = Quaternion.Euler (0, 270, 0);
}else {
DesiredRotation = Quaternion.Euler (0, 180, 0);
}
transform.rotation = Quaternion.Lerp (transform.rotation, DesiredRotation, Time.deltaTime*RotationSpeed);
}
answered Oct 19 at 9:33
Noblight
1165
1165
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26546664%2fhow-to-rotate-the-player-when-moving-your-mouse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown