Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Allign character with surface

Discussion in 'Scripting' started by riottaver, May 26, 2020.

  1. riottaver

    riottaver

    Joined:
    Apr 30, 2020
    Posts:
    7
    I'm doing this game in which two players controls two pirates, and they have to survive from objects falling from the sky while stading on the top of a plank, that works just like a seesaw.

    However, i've tried a lot to make the characters allign with the plank, but nothing seems to work, what i need is that the two characters normal vector to be the same as the normal vector of the plank , i think in the screenshot bellow you can see what i'm talking about

    equidou.png

    I've tried to make the rotation of the pirates to be the same as the plank, that kind of worked but whenever the plank moves the pirates kind slip on it, its hard to explain, but anyways, trying to equalize the rotation of both objects didnt work.

    The other way i've tried is with raycast, using their normal vectors, however i can't make it work either.

    If you guys could help me out that would be great, thank you
    PS: sorry for my english
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    I think this can work but I think you have to give the pirates a false offset. I took your screenshot and drew a green line that passes through the seesaw pivot. The pirate rotation should follow the plank rotation, but the pivots should be below them, and need to be precisely as deep as that same green axis.

    pirate_pivots.png

    It might just be easiest if you parent the pirates to the board and slide them smoothly left and right on LOCAL coordinates... don't know if they can jump, but that would make the jumps a bit weird...
     
  3. riottaver

    riottaver

    Joined:
    Apr 30, 2020
    Posts:
    7
    First thanks for the Reply!! So I tried to make the pivot in the green line just like you said, however it didnt work, they still slip a bit on the plank. Did i do something wrong or that is i should've done? And if the pivot thing didnt work, how can i parent them to the plank and make them move on the plank?
    pivot.png
     
  4. riottaver

    riottaver

    Joined:
    Apr 30, 2020
    Posts:
    7
    This is the code i'm using to equalize the rotation of both objects:

    ==========================================================================================
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Rotate : MonoBehaviour
    {
    public GameObject seesaw;
    public float seesawRotation;

    private void OnCollisionStay2D(Collision2D other) {
    if (other.gameObject.tag == "plank"){
    Allign();
    }
    }

    void Allign(){
    seesawRotation = seesaw.transform.eulerAngles.z;
    transform.eulerAngles = new Vector3(0, 0, seesawRotation);
    }
    }
    ===========================================================================================