Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by SteveKF · Jan 04, 2016 at 10:45 PM · physicscapsulebouncing

Make an object bounce when an object is walking on it

Hello Unity community,

I have a player/spider (red) which can make a path/thread and can also walk on it.

alt text

The capsule object which is connected (Rescale script see below) to two cubes should bounce a little bit when the player walks on it (like when a spider walks on a web).

The player can walk through the cubes. They're just there to help the user see where he can connect the next path. Is this even possible when the capsule is only one object?

The bouncing can be very simple. Nothing spectacular. However, without it it just feels wrong.

I hope someone can help me because I'm struggling with this.

 using UnityEngine;
 using System.Collections;
 
 public class Rescale : MonoBehaviour {
 
     public Transform start;
     public Transform end;
     public bool completed = false;
 
     public float factor = 0.5f;
 
     void Start() {
         SetPos(start.position, end.position);
     }
 
     void Update () {
         if (!completed) {
             SetPos (start.position, end.position);
         }
     }
 
     void SetPos(Vector3 start, Vector3 end) {
         var dir = end - start;
         var mid = (dir) / 2.0f + start;
         transform.position = mid;
         transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
         Vector3 scale = transform.localScale;
         scale.y = dir.magnitude * factor;
         transform.localScale = scale;
     }
 }

Player's Script:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class PathFollower : MonoBehaviour
 {
 
     public List<GameObject> path;
     public float speed = 5.0f;
     public float reachDist = 1.0f;
     public Transform reachedPoint = null;
     public float rotateSpeed = 3.0f;
 
     public Vector3 height;
 
     //empty prefab to represent one of the two ends of a spider thread
     public GameObject target;
     public Transform nextPoint = null;
 
 
 
     // Use this for initialization
     void Start()
     {
 
         path = gameObject.GetComponent<PlayerController>().points;
 
         //calculate the nearest point. This point will be the starting point
         int j = 0;
         float tmp = float.PositiveInfinity;
         for (int i = 0; i < path.Count; i++)
         {
             float distance = Vector3.Distance(transform.position, path[i].transform.position);
             if (distance < tmp)
             {
                 tmp = distance;
                 j = i;
             }
         }
 
         Transform startingPoint = path[j].transform;
         reachedPoint = startingPoint;
         height = new Vector3(0, startingPoint.lossyScale.y, 0);
         transform.position = startingPoint.position + height;
 
 
         //add script ReachablePoints to get all points the spider can reach from this specific point
         for (int i = 0; i < path.Count; i++)
         {
             path[i].AddComponent<ReachablePoints>();
         }
     }
 
 
     // Update is called once per frame
     void Update()
     {
 
         RaycastHit hit;
         int layerMask = 1 << 8;
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         Physics.Raycast(transform.position - height, fwd, out hit, float.PositiveInfinity, layerMask);
         if (hit.transform != null)
         {
             Debug.Log(hit.transform.gameObject.name);
         }
         nextPoint = hit.transform;
 
         //rotates if left or right are pressed down
         if (Input.GetKey("a"))
         {
             transform.Rotate(Vector3.down * rotateSpeed);
         }
         if (Input.GetKey("d"))
         {
             transform.Rotate(Vector3.up * rotateSpeed);
         }
 
 
         if (nextPoint != null && reachedPoint.GetComponent<ReachablePoints>().reachablePoints.Contains(nextPoint))
         {
 
             //calculate distance to the next path
             float dist = Vector3.Distance(nextPoint.position + height, transform.position);
 
             //move to the current Target if up or down are pressed
             if (Input.GetKey("w"))
             {
                 transform.position = Vector3.MoveTowards(transform.position, nextPoint.position + height, Time.deltaTime * speed);
             }
 
             if (Input.GetKey("s"))
             {
                 transform.position = Vector3.MoveTowards(transform.position, nextPoint.position + height, Time.deltaTime * speed);
             }
 
             //stop moving if the target point was reached
             if (dist <= reachDist)
             {
                 reachedPoint = nextPoint.transform;
             }
         }
     }
 }  


unity.png (19.5 kB)
Comment
Add comment · Show 5
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 ShadyProductions · Jan 04, 2016 at 10:47 PM -1
Share

$$anonymous$$aybe add some force on the rigidbody, but it will feel really weird if you don't make it timeout, like don't make it bounce every time it lands, make it like bounce ever 1 second after it lands orsomething, otherwise you'll get some really weird stuff happening to the player. Or you can just do an effect with the camera ins$$anonymous$$d?

avatar image SteveKF ShadyProductions · Jan 05, 2016 at 05:44 PM 0
Share

Thank you for your comment! What kind of force function do you think would be good? ForceAtPosition? You need to add a physics material to get the bounciness when you work with force, right?

avatar image incorrect · Jan 04, 2016 at 10:49 PM 0
Share

Dou you have player's script? To do this bouncing you probably have to send message to the path that it's being walked, so it can switch it's state to bouncing. Also it would be nice if the player will bounce with this path too as if it's staying on it.

And what type of bouncing do you want? Will it be enough to make it oscelate along vertical axis with sin function?

avatar image SteveKF incorrect · Jan 05, 2016 at 05:44 PM 0
Share

Hey thank you for your comment! I added the player script.

Yeah I think this will be enough. Do you have any idea how to do this?

avatar image incorrect SteveKF · Jan 06, 2016 at 01:34 AM 0
Share

It looks like your player moves not along the ribs of your graph, but rather in world coordinates. Am I missunderstanding something? Because I thought you are doing something like picking a route from point A to point B and moving along the way connecting them.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Softbodies in Unity 3D 1 Answer

Simple ball bounce (like pang/bubble trouble) 3 Answers

Issue using Vector3.Reflect to bounce a ball 1 Answer

Why does CapsuleCast appear in the wrong place? 0 Answers

How do I get triggers to work at a pixel perfect level? 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