- Home /
Movement from box to box
Hello,
Is it possible someone to help me with cube moving. The goal is when u click on your Phone screen (selecting the object(other box) the player to jump on it).
Thanks for the help!
Regards!
Answer by enerology · May 24, 2020 at 02:07 PM
If I understand correctly. Do you want the sphere in the image on the right to jump to the other platform when clicked? if so, then
1) I would implement a MouseClick function on the object you want to jump to.
2) Add an impulse force to the rigid body you want to make jump and this impulse would be straight up and distance from the other platform * the x,z components (if using for different distances of platforms later) Forgive me if my vector direction is wrong as I usually only work in 2d and my coordinates to move are x,y and in 3d space, I think they use x,z
public void jumpTrigger(GameObject objectToJumpTo)
{
float distanceScale = 1; //Increases distance influence on jump velocity
float jumpHorizontalSpeed = 1;
float jumpHeight = 1;
float distance = Vector3.Distance(objectToJumpTo.transform.position, yourGameobject.transform.position); //Increases jump distance
Vector3 vectorComponets = objectToJumpTo.transform.position - yourGameobject.transform.position; //Get angle in vector form between both objects
vectorComponets = vectorComponets.normalized;
yourRigidbody.AddForce(new Vector3(vectorComponets.x*(jumpHorizontalSpeed*(distance*distanceScale)),jumpHeight, vectorComponets.z*(jumpHorizontalSpeed * (distance * distanceScale))));
}
This may or may not work right off the bat, but should give you an idea on how to proceed. I also know that I could have left the normalized off, but it helps to see how the code snaps together to give horizontal velocity. Good luck with your project!
Hello @enerology! Thanks for your help. I can't only get the point on void jumpTrigger(GameObject objectToJumpTo). how to get this objectToJumpTo.
This is the all code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player$$anonymous$$oving : $$anonymous$$onoBehaviour
{
private Rigidbody yourRigidBody;
void Update()
{
if(Input.Get$$anonymous$$ouseButton(0))
{
jumpTrigger();
Debug.Log("click");
}
}
public void jumpTrigger(GameObject objectToJumpTo)
{
float distanceScale = 1; //Increases distance influence on jump velocity
float jumpHorizontalSpeed = 1;
float jumpHeight = 1;
float distance = Vector3.Distance(objectToJumpTo.transform.position, transform.position); //Increases jump distance
Vector3 vectorComponets = objectToJumpTo.transform.position - transform.position; //Get angle in vector form between both objects
vectorComponets = vectorComponets.normalized;
yourRigidBody.AddForce(new Vector3(vectorComponets.x*(jumpHorizontalSpeed*(distance*distanceScale)),jumpHeight, vectorComponets.z*(jumpHorizontalSpeed * (distance * distanceScale))));
}
}
and photo of unity Player settings:
Thank you again for your help.
All you need to do is add a reference to the Gameobject and assign it to the platform you want to jump to
public class Player$$anonymous$$oving : $$anonymous$$onoBehaviour
{
private Rigidbody yourRigidBody;
//Part I Added - in the editor, take the object you want to jump to and
//drag it onto this field in the editor
public GameObject objectToJumpToReference;
void Update()
{
if(Input.Get$$anonymous$$ouseButton(0))
{
jumpTrigger(objectToJumpToReference);
Debug.Log("click");
}
}
public void jumpTrigger(GameObject objectToJumpTo)
{
float distanceScale = 1; //Increases distance influence on jump velocity
float jumpHorizontalSpeed = 1;
float jumpHeight = 1;
float distance = Vector3.Distance(objectToJumpTo.transform.position, transform.position); //Increases jump distance
Vector3 vectorComponets = objectToJumpTo.transform.position - transform.position; //Get angle in vector form between both objects
vectorComponets = vectorComponets.normalized;
yourRigidBody.AddForce(new Vector3(vectorComponets.x*(jumpHorizontalSpeed*(distance*distanceScale)),jumpHeight, vectorComponets.z*(jumpHorizontalSpeed * (distance * distanceScale))));
}
}
This should work and if you need more help just ask. :)
@enerology, but how to assign cube which you click with the mouse on GameObject objectToJumpToReference and to count click only on the cubes?
First, you need to add this modified script that I just removed the update function from as we are now detecting clicks on the objects themselves.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class Player$$anonymous$$oving : $$anonymous$$onoBehaviour
{
private Rigidbody yourRigidBody;
//Part I Added - in the editor, take the object you want to jump to and
public void jumpTrigger(GameObject objectToJumpTo)
{
float distanceScale = 1; //Increases distance influence on jump velocity
float jumpHorizontalSpeed = 1;
float jumpHeight = 1;
float distance = Vector3.Distance(objectToJumpTo.transform.position, transform.position); //Increases jump distance
Vector3 vectorComponets = objectToJumpTo.transform.position - transform.position; //Get angle in vector form between both objects
vectorComponets = vectorComponets.normalized;
yourRigidBody.AddForce(new Vector3(vectorComponets.x * (jumpHorizontalSpeed * (distance * distanceScale)), jumpHeight, vectorComponets.z * (jumpHorizontalSpeed * (distance * distanceScale))));
}
}
Then you need to add this script to objects you want to jump to. This is going to detect the clicks on the object and send a reference of itself to the Player$$anonymous$$oving class. To use this, make sure the objects you are jumping to have a collider on them.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ClickController : $$anonymous$$onoBehaviour
{
GameObject Player;
public void On$$anonymous$$ouseDown()
{
Player.GetComponent<Player$$anonymous$$oving>().jumpTrigger(gameObject);
Debug.Log("click");
}
}
Now, for each object you add this trigger to, just add a reference to the player from the editor by dragging it into the Player field.
Good luck and if you need more help just let me know!
Your answer
![](https://koobas.hobune.stream/wayback/20220612235126im_/https://answers.unity.com/themes/thub/images/avi.jpg)