Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Duckname · May 24, 2020 at 09:45 AM · movementplayerbox

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).

alt text

Thanks for the help!
Regards!

jumpfromboxtobox.png (36.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 5 · Share
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 Duckname · May 24, 2020 at 02:35 PM 0
Share

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:

alt text

Thank you again for your help.

playersettings.png (62.9 kB)
avatar image enerology Duckname · May 24, 2020 at 06:07 PM 1
Share

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. :)

avatar image Duckname · May 24, 2020 at 02:37 PM 0
Share

I forgot to attach photo from console error: alt text

console-error.png (48.1 kB)
avatar image Duckname · May 26, 2020 at 05:43 PM 0
Share

@enerology, but how to assign cube which you click with the mouse on GameObject objectToJumpToReference and to count click only on the cubes?

avatar image enerology Duckname · May 26, 2020 at 06:33 PM 1
Share

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

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

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

Related Questions

Player Going through colliders 1 Answer

Box collider acting weird. 0 Answers

Orbit player around center point 1 Answer

Implement moveSpeed to this object script? 1 Answer

GUI movement Control. Please Help!!! Thanks 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