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

What code can i put?

Discussion in '2D' started by circleline, Jan 31, 2015.

  1. circleline

    circleline

    Joined:
    Nov 29, 2014
    Posts:
    42
    So..I want my 2d Object to be able to move only between -50 & 50 z axis. What code should i put?

    I did if(gameobject.transform.rotation.eulerangle.z == 50){

    speed = 0;

    }

    when it hits 50 it stops but i can't move back. why is that? what code should i put so it can move back and forth
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try, if(gameobject.transform.rotation.eulerangle.z > 50){
    gameobject.transform.rotation.eulerangle.z = 50}

    or show more of your code :)
     
  3. G4M5T3R

    G4M5T3R

    Joined:
    Jan 10, 2015
    Posts:
    10
    Wouldn't a simple Mathf.Clamp(transform.rotation.eulerangle.z, -50, 50) work for this? It's been a while since I've tried clamping a euler but I'm pretty sure it would achieve the desired effect.
     
  4. circleline

    circleline

    Joined:
    Nov 29, 2014
    Posts:
    42
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playerControl_B : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     bool death;
    8.  
    9.  
    10.  
    11.     // Use this for initialization
    12.    
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.        
    21.        
    22.         if (death == true)
    23.             speed = 0;
    24.        
    25.        
    26.         float vVal = Input.GetAxis ("PlayerOrbit_B") *  speed;
    27.        
    28.        
    29.    
    30.                    
    31.             transform.Rotate (0, 0, vVal);
    32.  
    That's my code ^ I've tried both and none of them work.