- Home /
How to rotate an object without transform.Rotate
I have made a character that can rotate around its axis. The question is: how can I make it rotate around its axis without altering the two scripts, and giving the velocity of RotateSpeed of TP_Motor It uses two scripts. The first script is called character controller. By the way i have simplified the code with only the most important parts. This is the script code:
using UnityEngine; using System.Collections;
public class TP_Controller : MonoBehaviour {
public static CharacterController CharacterController;
public static TP_Controller Instance;
void Awake()
{
CharacterController = GetComponent("CharacterController") as CharacterController;
Instance = this;
}
void Update()
{
GetLocomotionInput();
TP_Motor.Instance.UpdateMotor();
}
void GetLocomotionInput()
{
var deadZone = 0.1f;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
**// This is the part that should be altered//**
if (((Input.GetAxis("Horizontal") > deadZone) && !Input.GetKey(KeyCode.LeftShift)) ||
(Input.GetAxis("Horizontal") < -deadZone) && !Input.GetKey(KeyCode.LeftShift))
transform.Rotate (0, Input.GetAxis("Horizontal"), 0); **// <-- SPECIALLY THIS!! //**
TP_Animator.Instance.DetermineCurrentMoveDirection();
}
}
The second script is the TP_Motor. Here is the script:
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour {
public static TP_Motor Instance;
public float RotateSpeed = 2.0F;
public Vector3 MoveVector { 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);
}
public float MoveSpeed()
{
var MoveSpeed = 0f;
switch (TP_Animator.Instance.MoveDirection)
{
// Turning
case TP_Animator.Direction.TurningRight:
MoveSpeed = RotateSpeed;
break;
case TP_Animator.Direction.TurningLeft:
MoveSpeed = RotateSpeed;
break;
return MoveSpeed;
}
}
P.D. By the way i dont like transform.Rotate because it can make the character fall through the ground.
It is unclear what problem you are trying to solve here. You can use an empty game object and make your visible game object the child. Then you can use transform.localRotation to rotate your object.
I just want to find another way to rotate without the option transform.Rotate. is that simple. I hate transform. Isnt there a vector 3 or something?
There are a number of other ways of rotating, and many are more complex than transform.Rotate(). And there are potential pitfalls for many of them. Here is one simple way to set a rotation:
Vector3 v3Rotation = new Vector3(0.0f, 45.0f, 0.0f);
transform.eulerAngles = v3Rotation;
This sets an absolute rotation. transform.Rotate() is a relative rotation (i.e. it is added on to the current rotation).
Wow, the first option, is what i wanted to hear. Vector3 v3Rotation = new Vector3(0.0f, 45.0f, 0.0f); Now, how can i fix something like this: TP_$$anonymous$$otor.Instance.$$anonymous$$oveVector v3Rotation = new Vector3(0.0f, 45.0f, 0.0f); // It doesnt work!
Sorry. I used this:
if (((Input.GetAxis("Horizontal") > deadZone) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift)) ||
(Input.GetAxis("Horizontal") < -deadZone) && !Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))
TP_$$anonymous$$otor.Instance.v3Rotation = new Vector3(0.0f, 45.0f, 0.0f);
transform.eulerAngles = v3Rotation; // Doesnt contain a definition for v3Rotation.
Answer by JuanseCoello · Aug 08, 2013 at 10:51 PM
Ok, i solved the problem. This is what i am trying to do:
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???
Try:
transform.eulerAngles += new Vector3 (0,Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0);
The code is equivalent to:
transform.Rotate(0.0f, Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0.0f);
Your answer
Follow this Question
Related Questions
CharacterController.Move Not Corresponding to gameobject.transform.rotation 1 Answer
Problem with rotating a character that has faux gravity 1 Answer
Face direction to move in 2 Answers
How do i rotate based on direction character is moving in 1 Answer
Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers