- Home /
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?
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.
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.
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;
}
}
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);
}
Your answer
Follow this Question
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