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 mechanicarts · Jan 26, 2013 at 01:50 PM · rotatecharacter controller

Cannot rotate character controller

Hello. I want to rotate the camera (which is the child of an empty with a character controller) based on the collision of two other objects, a hand and a box. All the variables you see in the script are assigned from the inspector. The script does not work. It does not even log anything. The hand is a kinematic RB but I also tried with non-kinematic, still didn't work.

Also the script is attached to the hand.

 var turn:float;
 var par:CharacterController;
 var Rotator:GameObject;
 static var direction:int;
 
 function Update()
 {
     if (Rotator.name=="rotatorLeft")
     {
         direction = 0;
     }
     else
     {
         direction = 1;
     }
 }
 
 
 function onTriggerEnter(coll:Collider)
 {
     if (coll.gameObject==Rotator.gameObject)
     {
         Debug.Log("Rotate");
         if (direction==0)
         {
             par.transform.Rotate(Vector3.up*turn*Time.deltaTime, Space.World);
         }
         else
         {
             par.transform.Rotate(Vector3.up*turn*-1*Time.deltaTime, Space.World);
         }
         
     }
 }
Comment
Add comment · Show 2
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 FLASHDENMARK · Jan 26, 2013 at 02:29 PM 1
Share

Well... OnTriggerEnter is missing a capitalized O.

avatar image mechanicarts · Jan 26, 2013 at 02:56 PM 0
Share

Still does not work =(

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jan 26, 2013 at 03:10 PM

As @OrangeLightning said, it should be OnTriggerEnter - onTriggerEnter will never be called! But even with the correct event name, the movement will be so small that you may not notice it - that's because OnTriggerEnter only happens once when the object enters the trigger, thus multiplying the turn angle by Time.deltaTime will result in a ridiculous rotation.
If you want to rotate the object smoothly until it exits the trigger, do the following:

 var turn:float;
 var par:CharacterController;
 var Rotator:GameObject;
 static var direction:int; // are you sure this must be static?
 private var rot = false; // declare this boolean variable
 
 function Update()
 {
     if (Rotator.name=="rotatorLeft")
     {
        direction = 0;
     }
     else
     {
        direction = 1;
     }
     if (rot){ // rotate in Update while inside the trigger:
        var dir = 1;
        if (direction == 1) dir = -1;
        par.transform.Rotate(Vector3.up*turn*dir*Time.deltaTime, Space.World);
     }
 }
  
 function OnTriggerEnter(coll:Collider) // OnTrigger must have a capital O!
 {
     if (coll.gameObject==Rotator) // Rotator already is a game object reference
     {
        rot = true; // entered the trigger: start rotation
     }
 }
 
 function OnTriggerExit(coll:Collider)
 {
     if (coll.gameObject==Rotator)
     {
        rot = false; // leaving the trigger: stop rotation
     }
 }

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 mechanicarts · Jan 26, 2013 at 03:49 PM 0
Share

Direction should be private not static sorry. Also removed this code if (rot){ // rotate in Update while inside the trigger: var dir = 1; if (direction == 1) dir = -1; as it is wrong and couldn't understand what it did. Works exceptionally, like every time Aldo ^^

avatar image
1

Answer by Flickayy · Jan 26, 2013 at 02:44 PM

I'm not 100% sure about this, but in C# it needs to be inside a class inheriting Monobehaviour

I'll put my PlayerController here to show you. (C#)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     // Public player variables
     public GameObject bulletPrefab;
     public Transform turretTransform = null; 
     public Transform bulletSpawnPoint = null;
     
     public int playerLives = 3;
     public int playerShield = 4;
     public float playerVelocity = 1.5f;
     public float rotationSpeed = 70f;
     public float turretRotationSpeed = 100f;
     
     // Update is called once per frame
     void Update () {
         
         // Player input
         float playerMove = Input.GetAxis("Vertical") * playerVelocity * Time.smoothDeltaTime;
         float playerRotate = Input.GetAxis("Horizontal") * rotationSpeed * Time.smoothDeltaTime;
         
         float turretRotate = rotationSpeed * Time.deltaTime; 
 
         // Move player
         transform.Translate(0, 0, playerMove);
         transform.Rotate(0, playerRotate, 0);
         
         // Rotate turret
         if(Input.GetKey (KeyCode.Q)) {
             turretTransform.transform.Rotate(0, 0, -turretRotate);
         }
         if(Input.GetKey (KeyCode.E)) {
             turretTransform.transform.Rotate(0, 0, turretRotate);
         }
         
         if(Input.GetKey ("space")) {
             StartCoroutine(OnFire());
         }
     }
     
     
     void OnTriggerEnter(Collider hitEvent) {
         
         GameObject collision = hitEvent.gameObject;
         
         if (collision.name == "LifePowerUp") {
             print ("Gained a new Life!");
             playerLives++;
             Destroy(collision.gameObject);
         }
         
         if (collision.tag == "Enemy") {
             print("hit!");
             
             // begin checks for shields and lives
             if (playerShield > 0) {
                 playerShield--;
             }
             else if (playerShield == 0 && playerLives > 0) {
                 playerLives--;
             }
             else if (playerShield == 0 && playerLives == 0) {
                 print ("GameOver!");
             }
         }
         
         print(playerLives);
         
     }
     
     IEnumerator OnFire() {
         Instantiate (bulletPrefab, bulletSpawnPoint.position, turretTransform.rotation);
         yield return new WaitForSeconds(3);
     }
 }

Hope this helps.

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 mechanicarts · Jan 26, 2013 at 03:09 PM 0
Share

Thank you for taking the time to post this, but since I do not know any C# I cannot really decode this well enough to adapt to my problem ^^

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

12 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

Related Questions

Character controls seen in Amnesia 4 Answers

Lock Z Rotation. Character Controller. 1 Answer

palyer forward , side ways in running game 0 Answers

Camera rotation around player while following. 6 Answers

Roll a Ball using Character Controller 0 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