- Home /
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);
}
}
}
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
}
}
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 ^^
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.
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
Follow this Question
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