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 AKJ9 · Mar 30, 2014 at 05:26 PM · collisionrigidbodycollidermove an objectrigidbody-collision

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

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image

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


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