-
-
Save brismithSFHS/8dd4439b3857066665d3e69c5a3b5fac to your computer and use it in GitHub Desktop.
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
public class PlayerController : MonoBehaviour { | |
public float speed; | |
public float jumpHeight; | |
public Text countText; | |
public Text winText; | |
public int numPickups; | |
private Rigidbody rb; | |
private int count; | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody>(); | |
count = 0; | |
SetCountText(); | |
winText.text = ""; | |
} | |
void FixedUpdate () | |
{ | |
float moveHorizontal = Input.GetAxis("Horizontal"); | |
float moveVertical = Input.GetAxis("Vertical"); | |
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); | |
rb.AddForce(movement * speed); | |
if (Input.GetKeyDown("space")) | |
{ | |
Vector3 jump = new Vector3 (0.0f, jumpHeight, 0.0f); | |
rb.AddForce (jump); | |
} | |
} | |
void OnTriggerEnter(Collider other) | |
{ | |
if (other.gameObject.CompareTag("Pick Up")) | |
{ | |
other.gameObject.SetActive(false); | |
count++; | |
SetCountText(); | |
} | |
} | |
void SetCountText () | |
{ | |
countText.text = "Count: " + count.ToString (); | |
if (count >= numPickups) | |
{ | |
winText.text = "You win!"; | |
} | |
} | |
} |
@brismithSFHS Never mind it works now I was using the wrong version of Visual Studios lol
Thx
:D
I am trying to make a ball move:
I put in this exact code (below) but it's not working, I'm using Unity Version 2020.2.0b12,3574:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControllerBase
{
}
public class PlayerController : MonoBehaviour
{
public float speed = 0;
private Rigidbody rb;
private float movementX;
private float movementY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
private void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
}
PLS HELP 😞
@Galaxy-EM6
I think you may have found it out by now and I probably don't know much more than you but, In the unity editor have you clicked save asset inside of your Input actions ? If so, after that you need to select your Input actions in the Assets and tell it to generate a c# class, you can leave the fields blank, then press apply. And as far as the code goes I don't see any reason for it to not work except that currently your speed is set to 0 (unless you changed it in the editor) and your player character might not have a rigidbody or the rigidbody is part of the child. Also, most importantly, make sure there is a player Input component with the Input actions on it attached to the game object.
I used your code and that was the only thing I needed to do.
It no work lol