- Home /
Question by
karpiq · Nov 15, 2013 at 10:37 PM ·
collisioncharactercontrollerdetection
CharacterController collision detection with pendulum like object
This is my code for pendulum motion of a cuboid:
using UnityEngine;
using System.Collections;
public class YouSpinMeRound : MonoBehaviour {
public float[] angle= new float[3];
public float[] speed= new float[3];
public bool[] axis = new bool[3];
private bool[] left= new bool[3];
void OnControllerColliderHit( ControllerColliderHit hit)
{
Debug.Log("Character hit!");
}
void Update () {
if(axis[0])
{
if(left[0])
{
transform.Rotate(Time.deltaTime * speed[0],0,0);
if(transform.rotation.x >= angle[0])
left[0]=false;
}
else
{
transform.Rotate(-Time.deltaTime * speed[0],0,0);
if(transform.rotation.x <= -angle[0])
left[0]=true;
}
}
if(axis[1])
{
if(left[1])
{
transform.Rotate(0, Time.deltaTime * speed[1],0);
if(transform.rotation.y >= angle[1])
left[1]=false;
}
else
{
transform.Rotate(0, -Time.deltaTime * speed[1],0);
if(transform.rotation.y <= -angle[1])
left[1]=true;
}
}
if(axis[2])
{
if(left[2])
{
transform.Rotate(0,0, Time.deltaTime * speed[2]);
if(transform.rotation.z >= angle[2])
{
left[2]=false;
}
}
else
{
transform.Rotate(0,0, -Time.deltaTime * speed[2]);
if(transform.rotation.z <= -angle[2])
{
left[2]=true;
}
}
}
}
}
And this is script for detecting collision on player:
using UnityEngine;
using System.Collections;
public class ImpactReceiver : MonoBehaviour {
CharacterController character;
void Start ()
{
character=GetComponent<CharacterController>();
}
void OnCollisionEnter( Collision col)
{
Debug.Log("Collision!");
}
}
OnCollisionEnter in ImpactReceiver and OnControllerColliderHit both don't show any results when cube hits player. So I'd like to know how to do this kind of motion on object and detect when player gets hit by it and then maybe push him or sometihng. My cube has rigidbody with gravity disabled so it would'nt fall.
Comment