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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Turning 2.5D

Discussion in 'Scripting' started by cjtouhey, Sep 27, 2016.

  1. cjtouhey

    cjtouhey

    Joined:
    Apr 5, 2016
    Posts:
    11
    So ive been pulling my hair out all day trying to figure this out. i have looked all over google,
    anyways.

    what i want my controller to do is rotation (0,180,0) once after click ("a") then if the players already facing (0,180,0) disable the rotation until it pivots (0,180,0) again by clicking the ("d") key a simple concept.

    The ways i have thought of tackling the situation is to have ("a") and ("d") using the transform.LookAt command with a vector3, however as im trying to use the vector3 as variable it keeps returning the type used like a variable error.

    The other way i was thinking is to use something like
    Code (CSharp):
    1. if  vector 3 y axis is at (0,180,0) roatation = false
    How ever im not sure how to do this.

    Anyhelp would be awesome

    Thanks.
     
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Hey there, I think that your problem is a bit hard to understand.
    Can you provide some drawings of what you are trying to achieve? Also, can you show us the problematic code? (the one that throws "like a variable error")
     
  3. cjtouhey

    cjtouhey

    Joined:
    Apr 5, 2016
    Posts:
    11
    Ok so basicly, the script turns the player back and fowards. in a 2.5D world so ("a") would be back and ("d") foward. Like this
    Code (CSharp):
    1. voud Update ()
    2. {
    3.   if (Input.GetKeyDown("a"))
    4.      transform.Rotate (0,180,0);
    5. // this would transform the object 180 degrees every time ("a") was pushed//
    6.      
    7. }
    How ever if i want my player to turn 180 degrees left, and then continue walking, the next time i click ("a") to walk the character will rotate 180 degrees back to its right facing position.

    The idea i had to stop this was using something like
    Code (CSharp):
    1. if (Input.GetKeyDown("d"))  {
    2. transform.LookAt (target); }
    3.  
    4. if (Input.GetKeyDown("a"))  {
    5. transform.LookAt (othertarget); }
    how ever i dont know how to implment this function to look at something, and im not sure how efficient it is doing it this way.

    does this help?


    Cheers for your reply

    Chris
     
  4. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Does this help you?
    Code (CSharp):
    1.         if(Input.GetKeyDown(KeyCode.A))
    2.             transform.eulerAngles = new Vector3(0, 180, 0);
    3.         if(Input.GetKeyDown(KeyCode.D))
    4.             transform.eulerAngles = new Vector3(0, 0, 0);
     
  5. cjtouhey

    cjtouhey

    Joined:
    Apr 5, 2016
    Posts:
    11
    unfortunatly not. I tried something simular before! both ended with the character turning 90 degrees and walking through the background. ill post the full code and diagram. Cheers for sticking this out btw!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.  
    7.     public float walk = .7f;
    8.     public Animation anim;
    9.  
    10.     void Start ()
    11.     {
    12.    
    13.     anim = GetComponent<Animation>();
    14.  
    15.     }
    16.     void Update()
    17.  
    18.     {
    19.         var x = Input.GetAxis("Horizontal") * Time.deltaTime * walk;
    20.         var z = Input.GetAxis("Vertical") * Time.deltaTime * walk;
    21.  
    22.         transform.Rotate(0, 0, 0);
    23.         transform.Translate(0, 0, x);
    24.    
    25.         anim ["walk_normal"].speed = 2f;
    26.  
    27.  
    28.         if(Mathf.Abs(Input.GetAxis("Horizontal"))>0.1F)
    29.             anim.Play("walk_normal");
    30.            
    31.  
    32.         else
    33.             anim.Play ("idle_normal");
    34.  
    35.         if(Input.GetKeyDown(KeyCode.A))
    36.             transform.eulerAngles = new Vector3(0, 180, 0);
    37.         if(Input.GetKeyDown(KeyCode.D))
    38.             transform.eulerAngles = new Vector3(0, 0, 0);
    39.  
    40.  
    41.  
    42.         }
    43.  
    44.         }
    45.  
    46.        
     
  6. Antares_Insomnious

    Antares_Insomnious

    Joined:
    Dec 31, 2016
    Posts:
    1
    I'm sure I'm late, but this might help someone else.
    if you aren't using an animation (haven't gotten the animation portion to work yet) and just want the character to rotate 180 degrees. then you can use.
    Hope it helps someone out there...
    The code just states that the forward direction is equal to the Vector3 movement.
    so if movement = 1, 0, 0;
    then forward will be in that direction.

    Code (CSharp):
    1.  
    2.  
    3. private Vector3 movement;
    4.  
    5. private void FixedUpdate()
    6. {
    7.       float h = Input.GetAxis("Horizontal");
    8.       Walk(h);
    9.       AnimateWalk(h);
    10.       TurnAround(h);
    11. }
    12.  
    13. private void Walk(float h)
    14.     {
    15.         movement.Set(h, 0f, 0f);
    16.         movement = movement.normalized * walkSpeed * Time.deltaTime;
    17.         rb.MovePosition(transform.position + movement);
    18.     }
    19.  
    20. private void AnimateWalk(float h)
    21.     {
    22.         bool walking = h != 0;
    23.         anim.SetBool("IsWalking", walking);
    24.     }
    25.  
    26. private void TurnAround(float h)
    27. {
    28.       if(h > 0 || h < 0)
    29.       {
    30.            transform.forward = movement;
    31.       }
    32. }
    33.  
     
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You could also just add a bool to keep track of his facing
    Code (CSharp):
    1. bool facingForward = true;
    2.  
    3. if(Input.GetKeyDown(KeyCode.A) && facingForward)
    4. {
    5.             transform.eulerAngles = new Vector3(0, 180, 0);
    6.             facingForward = false;
    7. }
    8.         if(Input.GetKeyDown(KeyCode.D) &&!facingForward)
    9.        {
    10.             transform.eulerAngles = new Vector3(0, 0, 0);
    11.             facingForward = true;
    12.       }
     
  8. abdoon-105

    abdoon-105

    Joined:
    Feb 16, 2015
    Posts:
    3
    Try this code:
    Code (CSharp):
    1.  
    2. public float speed;
    3.  
    4. void FixedUpdate()
    5. {
    6.     if (Input.GetAxisRaw ("Horizontal") == 0)
    7.         return;
    8.  
    9.     Vector3 movement = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
    10.     Quaternion targetRotation = Quaternion.LookRotation (movement, Vector3.up);
    11.     transform.rotation = Quaternion.Lerp (
    12.         transform.rotation,
    13.         targetRotation,
    14.         Time.deltaTime * speed
    15.     );
    16. }
    17.  
    You can also use ThirdPersonCharacter.cs and ThirdPersonUserControl.cs in the Characters package, and do not forget to set the vertical axis value to zero.
     
    Last edited: Feb 11, 2017
    Jroel likes this.
  9. Kungfujesus

    Kungfujesus

    Joined:
    Aug 30, 2017
    Posts:
    4
     
  10. Kungfujesus

    Kungfujesus

    Joined:
    Aug 30, 2017
    Posts:
    4
    alright dude, did you solve your problem? Cloud Kids code is good,
    1. if(Input.GetKeyDown(KeyCode.A))
    2. transform.eulerAngles = new Vector3(0, 180, 0);
    3. if(Input.GetKeyDown(KeyCode.D))
    4. transform.eulerAngles = new Vector3(0, 0, 0);
    Remember your character is rotated 90 degrees already for 2.5d as it's a sidescroller so it would be
    1.if(Input.GetKeyDown(KeyCode.A))
    2. transform.eulerAngles = new Vector3(0,270,0);
    3.If(Input.GetKeyDown(KeyCode.D))
    4. transform.eulerAngles = new Vector3(0,90,0);

    hope this helps