- Home /
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.
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;
}
}
}
}
$$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?
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?
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?
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?
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.