2.Add the 3d assets + first few lines of code

    1. If you want, drag a different material from Course Library > Materials onto the Ground object
    2. Drag 1 Human, 3 Animals, and 1 Food object into the Hierarchy
    3. Rename the character “Player”, then reposition the animals and food so you can see them
    4. Adjust the XYZ scale of the food so you can easily see it from above

 

    • Attach the script to the Player and open it
    • In your Assets folder, create a “Scripts” folder, and a “PlayerController” script inside
    • At the top of PlayerController.cs, declare a new
    •  [SerializeField]
    • private float horizontalInput
    • In Update(), set horizontalInput = Input.GetAxis(“Horizontal”), then test to make sure it works in the inspector
using UnityEngine;

public class Movement : MonoBehaviour
{
    [SerializeField]
    private float horizontalInput;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
    }
}

 

 

4.Move the player left-to-right


using UnityEngine;

public class Movement : MonoBehaviour
{
    [SerializeField]
    private float horizontalInput;
    [SerializeField]
    private float speed = 30.0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);
    }
}

 

5+6. Keep the player inbounds

New Functionality

The player can move left and right based on the user’s left and right key presses

The player will not be able to leave the play area on either side
New Concepts & Skills
Adjust object scale
If-statements
Greater/Less than operators
Next Lesson, We’ll learn how to create and throw endless amounts of food to feed our animals!


now use chat gpt to teach me thing and fix a bug that can occur when you move fast

float clampedX = Mathf.Clamp(desiredpoz.x, -Xrange, Xrange) become the min/max

using UnityEngine;
//needed a bit of editing coz cgpt3.5 is stoooooooopid
public class Movement : MonoBehaviour
{
    [SerializeField]
    private float speed = 10.0f;
    [SerializeField]
    private float xRange = 10.0f;
    [SerializeField]
    private float horizontalInput;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");

        // Calculate the desired position based on the input
        Vector3 desiredPosition = transform.position + Vector3.right * -horizontalInput * Time.deltaTime * speed;

        // Clamp the desired position within the x range
        float clampedX = Mathf.Clamp(desiredPosition.x, -xRange, xRange);
        desiredPosition = new Vector3(clampedX, desiredPosition.y, desiredPosition.z);
        // Move the object to the clamped position
        transform.position = desiredPosition;
    }
}

Revision #2
Created 20 May 2023 23:47:54 by naruzkurai
Updated 18 June 2023 09:54:37 by naruzkurai