- Home /
How to make an object 'push' my character out of the way upon collision?
Hi all,
I'm on a fairly basic level at using Unity and have created a decent game so far. However, I am having an issue with a form of collision. I have two objects, Mario (my controllable character) and a Blue Shell that moves around four different waypoints.
What I would like to happen is for the Blue Shell to push/blast Mario out of the way should Mario stand in its path. The Shell's movement should not be altered (i.e. should not be slowed down or distorted) and should continue to head for the next waypoint. Mario on the other hand, should be blasted out of the way.
I have experimented with applying colliders and rigid-bodies to each object, but have had no luck. Both objects have character controllers.
In case it's useful, below is some script for my shell's movement behaviour:
using UnityEngine;
using System.Collections;
public class ShellController : MonoBehaviour
{
// speed of the AI player
public int speed = 4;
// speed the ai player rotates by
public int rotationSpeed = 5;
// the waypoints
public Transform[] waypoints;
// current waypoint id
private int waypointId = 0;
private CharacterController characterController;
void Start()
{
// retrieve this GameObjects CharacterController Component and store in characterController (todo)
characterController = gameObject.GetComponent<CharacterController>();
}
void Update()
{
Patrol ();
}
void Patrol()
{
// if no waypoints have been assigned
if (waypoints.Length == 0)
{
Debug.LogError("You need to assign some waypoints within the Inspector!");
return;
}
// if distance to waypoint is less than 2 metres then start heading toward next waypoint
if (Vector3.Distance(waypoints[waypointId].position, transform.position) < 2)
{
// increase waypoint id
waypointId++;
// make sure new waypointId isn't greater than number of waypoints
// if it is then set waypointId to 0 to head towards first waypoint again
if (waypointId >= waypoints.Length) waypointId = 0;
}
// move towards the current waypointId's position
MoveTowards(waypoints[waypointId].position);
}
/**
Move towards the targetPosition
*/
void MoveTowards(Vector3 targetPosition)
{
// calculate the direction to waypoint
Vector3 direction = targetPosition - transform.position;
// rotate over time to face the target rotation - Quaternion.LookRotation(direction)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (direction), rotationSpeed * Time.deltaTime);
// set the x and z axis of rotation to 0 so the agent stands upright (otherwise equals REALLY bad leaning)
transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
// use the CharacterController Component's SimpleMove(...) function
// multiply the agent's forward vector by the speed to move the AI
characterController.SimpleMove (transform.forward * speed);
}
}
Your help would be much appreciated!
Many thanks
Your answer
Follow this Question
Related Questions
Rigidbody not colliding with walls 1 Answer
rigid bodies passing through one another. 1 Answer
How to speed up? 3 Answers
Maintain velocity after impact with kinematic rigidbody 1 Answer
Fast object on collider not working 1 Answer