- Home /
Set a relative (custom) speed to a rotation
I am making a character rotate around its axis using euler angles. Not quite as transform.Rotate that makes the character fall through the terrain. But I want it to put a custom speed. I mean a float or something that can be edited in the script for the character to rotate at a speed I want to set in the script.
if (((Input.GetAxis("Horizontal") > deadZone) && !Input.GetKey(KeyCode.LeftShift)) ||
(Input.GetAxis("Horizontal") < -deadZone) && !Input.GetKey(KeyCode.LeftShift))
transform.eulerAngles += new Vector3 (0, Input.GetAxis("Horizontal"), 0);
It means that if i press the left or right inputs, the character rotates around its y axis. Now i have to set its speed. How could I set a relative speed to it???
Like this:
if (((Input.GetAxis("Vertical") > deadZone) && !Input.GetKey(KeyCode.LeftShift)) ||
(Input.GetAxis("Vertical") < -deadZone && !Input.GetKey(KeyCode.LeftShift)))
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
This is the other code from the other script:
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set; }
void Awake ()
{
Instance = this;
}
public void UpdateMotor () // Le cambie era UpdateMotor
{
ProcessMotion();
}
void ProcessMotion()
{
// First Movevector
// Transform MoveVector to worldspace
MoveVector = transform.TransformDirection(MoveVector);
// Normalize MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed();
// Reapply VerticalVelocity MoveVector.y
MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
// Apply Gravity
ApplyGravity();
// Move the character into world space
TP_Controller.CharacterController.Move(MoveVector * Time.deltaTime);
}
Answer by clunk47 · Aug 08, 2013 at 11:16 PM
If you want to rotate at a certain speed depending on your horizontal axis, set a speed to base your input on:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
float speed = 3.0f;
void Update()
{
transform.localEulerAngles += new Vector3(0, Input.GetAxis ("Horizontal") * speed * Time.deltaTime, 0);
}
}
Always happy to help, please accept the answer if it resolved your concern :D
Your answer
Follow this Question
Related Questions
transform.eulerAngles rotating speed (quick question) 1 Answer
eulerAngles.y always returns 179.9999 1 Answer
Worldspace EulerAngle.x regardless of z rotation ? 3 Answers
How to rotate 90 degrees from 270 degrees to 0 with lerp? c# 1 Answer
Camera viewport transformation from one world to the rotated world. 1 Answer