- Home /
Collision go through help?
I have my main player and I want him so that when he touches a certain object, he appears at another one. I tried using this script to no avail:
using UnityEngine;
using System.Collections;
public class Fun : MonoBehaviour {
float speed = 5.0f;
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "Player"){
transform.LookAt(target);
transform.position += transform.forward * speed * Time.deltaTime;
}
}
}
Help will be appreciated!
Hey Fencer300, any progress on this? If any of the answers helped you, would you $$anonymous$$d marking them as answered, so that others will be able to get help if they run into the same problem. If you're still fighting this, could you give us an update, so we can try to help you? Thanks.
Answer by Memige · Jun 01, 2013 at 07:59 PM
The Script you have there should (assuming proper setup) cause your trigger object to face the target location and move toward it for one frame. If you want to teleport your Player to the other location, instead of adding a small forward vector to the trigger object, just directly set the Player's position to the target's. Like so:
col.transform.position = target.position;
Answer by aldonaletto · Jun 01, 2013 at 08:01 PM
Well, this code doesn't seem to do anything remotely similar to what you want - at most the object will move a few milimeters in direction to target when the player touches it.
From your text, it seems that you actually want to teleport the player when it touches this object - is this right? If so, you should make this object a trigger and use OnTriggerEnter: when the player touches the trigger, the code instantly moves him to the target object's position, copying the target rotation as well:
using UnityEngine;
using System.Collections;
public class Fun : MonoBehaviour {
public Transform target; // drag the destination empty object here
void OnTriggerEnter(Collider other) {
if (other.tag == "Player"){ // if the player touched the trigger...
other.transform.position = target.position; // move it to the target position...
other.transform.rotation = target.rotation; // and assume the target rotation too
}
}
}
Answer by Cowboykilla · Jun 01, 2013 at 09:11 PM
Memige is exactly right with the teleportation script. As for moving... I was thinking maybe just have a static boolean variable called "moving" or something like that preset to false. Then on the Update call, apply the movement portion within an if statement. Also, within that statement have a nested if stating that if the player's location is as close to the target as you'd like, turn off the moving. Something like:
if (moving==true)
{
//Move Script Goes Here
if (Distance between Player and Target <= 3) // If the player is within a 3 unit radius
{
moving=false;
}
}
Hope this helps.
Your answer
Follow this Question
Related Questions
Character falling but stopped by rigidbody obstacle? 0 Answers
Making a cube fall without rigidbody 1 Answer
the ball falls in a straight line sloping 0 Answers
moving platforms 1 Answer