1. Post #1

    August 2011
    74 Posts
    Hello, I'm trying to make my moving platform with this code:
    Code:
    using System.Collections;
    
    public class testfloater:MonoBehaviour {
    public float Speed = 0.2F;
    public float stopper = 50.0F;
    public float header = 0.0F;
    
    	
    
    
    	// Use this for initialization
    	void Start () {
    	
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	   
    		
    	    float moveInput = Time.deltaTime * Speed; 
            transform.position += new Vector3(moveInput, 0, 0);
    	    
    		if (transform.position.x = stopper)
    		{
    		 float inverseInput = Time.deltaTime * Speed * -1.0F;
             transform.position += new Vector3(inverseInput, 0, 0);
    		}
    		
    		if (transform.position.x = header)
    		{
    		 transform.position += new Vector3(moveInput, 0, 0);
    		}
    		
    	
    }
    }
    But the output say's I cannot modify the return value of "UnityEngine.Transform.position" and that I cannot change a float to a bool. Any suggestions?

  2. Post #2
    AtomiCasd's Avatar
    June 2011
    588 Posts
    Your code looks so ugly a paper bag wouldn't even help.
    Reply With Quote Edit / Delete Reply Windows 7 Norway Show Events Dumb Dumb x 15Agree Agree x 3Programming King Programming King x 1Funny Funny x 1 (list)

  3. Post #3
    Stary2001's Avatar
    July 2011
    40 Posts
    Code:
    if (transform.position.x = stopper)
    needs to be

    Code:
    if (transform.position.x == stopper)
    the same with the other if

    = is assignment, == is comparison.
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Agree Agree x 4Programming King Programming King x 2Useful Useful x 1 (list)

  4. Post #4

    August 2011
    74 Posts
    Code:
    if (transform.position.x = stopper)
    needs to be

    Code:
    if (transform.position.x == stopper)
    the same with the other if

    = is assignment, == is comparison.
    Removed the errors, but now the platform doesn't stop, It just keeps going
    Tried this:
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class testfloater:MonoBehaviour {
    public float Speed = 0.2F;
    public float stopper = 50.0F;
    public float header = 0.0F;
    
    	
    
    
    	// Use this for initialization
    	void Start () {
    	
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	   
    		 
    	    float moveInput = Time.deltaTime * Speed; 
            transform.position += new Vector3(moveInput, 0, 0);
    	    
    		if (transform.position.x == stopper)
    		{
    		 moveInput = moveInput * -1.0F;
             transform.position += new Vector3(moveInput, 0, 0);
    		}
    		
    		if (transform.position.x == header)
    		{
    	     moveInput = moveInput * -1.0F;
    		 transform.position += new Vector3(moveInput, 0, 0);
    		}
    		
    	
    }
    }
    Still having the same problem.

  5. Post #5
    Stary2001's Avatar
    July 2011
    40 Posts
    I think this might work better for Update()

    Code:
    void Update () 
    {
        transform.position += new Vector3(Time.deltaTime * Speed, 0, 0); // apply movement
    	    
    	if ((transform.position.x > stopper) ||(transform.position.x < header)) // if we are past either of these then
    	{
    		transform.position -= new Vector3(Time.deltaTime * Speed, 0, 0); // undo the move
    		Speed=-Speed; // go the other way by negating speed
    	}
    }
    tested it in unity just now and it works!
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Programming King Programming King x 4 (list)

  6. Post #6

    August 2011
    74 Posts
    I think this might work better for Update()

    Code:
    void Update () 
    {
        transform.position += new Vector3(Time.deltaTime * Speed, 0, 0); // apply movement
    	    
    	if ((transform.position.x > stopper) ||(transform.position.x < header)) // if we are past either of these then
    	{
    		transform.position -= new Vector3(Time.deltaTime * Speed, 0, 0); // undo the move
    		Speed=-Speed; // go the other way by negating speed
    	}
    }
    tested it in unity just now and it works!
    Awesome dude thanks!

  7. Post #7
    Stary2001's Avatar
    July 2011
    40 Posts
    If you have any other problems, you can PM me and I'll help.
    Reply With Quote Edit / Delete Reply Windows Vista United Kingdom Show Events Friendly Friendly x 5 (list)

  8. Post #8
    Gold Member
    Eric95's Avatar
    January 2009
    4,576 Posts
    I usually use Javascript in Unity, I've tried some C# and I can't really find any differences except for slightly different syntax and a function being called a void. Are there any huge differences?

  9. Post #9
    Gold Member
    Ortzinator's Avatar
    May 2005
    1,645 Posts
    I usually use Javascript in Unity, I've tried some C# and I can't really find any differences except for slightly different syntax and a function being called a void. Are there any huge differences?
    Static typing alone makes it worth switching IMO.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 3 (list)

  10. Post #10
    Gold Member
    Darwin226's Avatar
    January 2009
    3,873 Posts
    I usually use Javascript in Unity, I've tried some C# and I can't really find any differences except for slightly different syntax and a function being called a void. Are there any huge differences?
    Void just means the function returns nothing.
    If it returned a number for example it would say int, double or float.
    Reply With Quote Edit / Delete Reply Windows 7 Croatia Show Events Agree Agree x 1 (list)

  11. Post #11

    October 2011
    35 Posts
    Your code looks so ugly a paper bag wouldn't even help.
    Why even bother commenting?
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Agree Agree x 2 (list)

  12. Post #12
    Gold Member
    Nisd's Avatar
    October 2008
    433 Posts
    Why even bother commenting?
    Why do you?
    Reply With Quote Edit / Delete Reply Windows 7 Denmark Show Events Dumb Dumb x 4 (list)

  13. Post #13
    Gold Member
    BackwardSpy's Avatar
    May 2008
    6,124 Posts
    Why do you?
    For exactly the same reason you did.

  14. Post #14
    IndieGamer's Avatar
    February 2012
    65 Posts
    Your code looks so ugly a paper bag wouldn't even help.
    Says the guy who's profile picture is a guy wearing a gas mask? He must be pretty ugly.
    Reply With Quote Edit / Delete Reply Windows 7 United Kingdom Show Events Dumb Dumb x 7 (list)