Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JuanseCoello · Aug 08, 2013 at 09:25 PM · rotationtransformcharactercontrollerlocomotion system

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 08, 2013 at 10:01 PM 0
Share

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.

avatar image JuanseCoello · Aug 08, 2013 at 10:05 PM 0
Share

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?

avatar image robertbu · Aug 08, 2013 at 10:13 PM 1
Share

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

avatar image JuanseCoello · Aug 08, 2013 at 10:16 PM 0
Share

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!

avatar image JuanseCoello · Aug 08, 2013 at 10:23 PM 0
Share

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.
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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???

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 09, 2013 at 01:28 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges