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
1
Question by spreadyourwings · Dec 28, 2021 at 05:56 AM · rigidbody3dgamephisics

How to make object get affected by a moving object?

I am making a game that the player run's on top the the train. However the player is not affected by a moving grounds and just stand on top of it. How can I make it so that the player get affected by a moving train?

Comment
Add comment · Show 3
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 endasil_unity · Dec 28, 2021 at 04:48 PM 0
Share

Sorry but this is a bit like asking a question like "I once saw a blue car in New York City, who owns it?" There are not nearly enough details to answer your question with this little information. There are so many ways that a player and train can be implemented and we can't know your specific case with the information you provided.

Take a look at this video for information on how to write good questions https://www.youtube.com/watch?v=ezAPpViLs2Q and read this https://mattgemmell.com/what-have-you-tried/ then hopefully you can provide enough information for us to understand how to help you.

avatar image spreadyourwings endasil_unity · Dec 29, 2021 at 01:35 AM 0
Share

Thank you this was my first time posting question in Unity Answers so this video Taught me a lot of things about this website and both video and website taught me about English and how to ask questions in real life. Thank You for your help.

avatar image spreadyourwings · Dec 29, 2021 at 01:40 AM 0
Share

This is my code for moving the train

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveRight : MonoBehaviour
 {
     private float speed = 5;
     
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         MoveRight();
 
         CheckDelete();
     }
 
     private void MoveRight()
     {
         transform.Translate(Vector3.right * speed * Time.deltaTime);
     }
 
     private void CheckDelete()
     {
         if (transform.position.x > 30)
         {
             Destroy(gameObject);
         }
     }
 }

this is my code for moving the player

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovementContoller : MonoBehaviour
 {
     [SerializeField] private float speed;
     [SerializeField] private float jumpForce;
 
     private Rigidbody rb;
     private bool isOnGround = true;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     private void Update()
     {
         Jump();
     }
 
     private void FixedUpdate()
     {
         Move();
     }
 
     private void Move()
     {
         float hAxis = Input.GetAxisRaw("Horizontal");
         float vAxis = Input.GetAxisRaw("Vertical");
 
         Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.fixedDeltaTime;
 
         Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement);
 
         rb.MovePosition(newPosition);
     }
 
     private void Jump()
     {
         if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
         {
             rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
             isOnGround = false;
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         isOnGround = true;
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by endasil_unity · Dec 29, 2021 at 09:54 AM

Great, now it's much easier to help. A simple solution to this is to set the player to have the train as its parent transform when the player collides with it and remove the trains parent status when the player leaves. Setting a parent means that any movement of the parent will also move the child in the same way. I added some example code here, written from my phone so there may be some minor errors but the general idea should be clear

Make sure your train has a RigidBody, if it does not have one. Mark the RigidBody as Kinematic so that it's not affected by physic forces like gravity

To your train script add an OnCollisionEnter and OnCollisionExit function todo that, like this:

 private void OnCollisionEnter(Collision collision)
 {
     Debug.Log("Player collided with train");
     collision.transform.SetParent(transform);
 }
 
 private void OnCollisionExit(Collision collision)
 {
     Debug.Log("Player left  the train");
     collision.transform.SetParent(null);
 }
Comment
Add comment · 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
0

Answer by spreadyourwings · Dec 29, 2021 at 08:49 AM

I put my code in a comment section

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

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

237 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 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

How to creat a scenario for a mobile game 0 Answers

slide sphere on a slope without rotating 1 Answer

Throwing a Spear 1 Answer

How do I add ForceMode.VelocityChange to this code 1 Answer

How can I add smooth movement to a RigidBody? 2 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