Unity Player movement script getting error CS1061 - c#

I am trying to get a player movement script working using the one on Unity's website but it does not work. im getting the error CS1061.
"Assets\Scripts\PlayerMovement.cs(44,21): error CS1061: 'float' does not contain a definition for 'y' and no accessible extension method 'y' accepting a first argument of type 'float' could be found (are you missing a using directive or an assembly reference?)
"
I checked the internet but with my limited knowledge of unity i couldnt quite figure it out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
private float playerSpeed = 2.0f;
private float jumpHeight = 1.0f;
private float gravityValue = -9.81f;
// Start is called before the first frame update
void Start()
{
controller = gameObject.AddComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
playerSpeed.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}
this is my code. If anyone can figure out what is wrong? Apparently the problem is something with Classes. But i have no idea what 'y' has to do with Classes.

playerSpeed.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
I believe this is supposed to be playerVelocity instead?
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);

Related

Unity AI character controller gravity

I am trying to add gravity to my "residents" of a town builder game. Basically the residents just walk around to a random location, wait 2 seconds then do it again. I am trying to add gravity to them so they don't float.
controller = GetComponent<CharacterController>();
moveDirection = Vector3.zero;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
These four lines are the gravity lines I tried added and very weird things happen such as the resident teleporting up if it touches anything, even the ground. What can I do? I basically just want the residents not to float above the ground and follow the slopes of the land like regular walking.
Below is the whole code if its needed to help solve my problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Resident : MonoBehaviour
{
private Vector3 location;
private Quaternion rotation;
private int speed;
private Vector3 moveDirection = Vector3.zero;
private bool canRotate = true;
Vector3 moveVector;
CharacterController controller;
// Start is called before the first frame update
void Start()
{
transform.rotation *= Quaternion.Euler(-90, 0, 0);
controller = GetComponent<CharacterController>();
speed = 5;
SetRandomPos();
StartCoroutine(ExampleCoroutine());
}
void Update()
{
int gravity = 20;
moveDirection = Vector3.zero;
//Check if cjharacter is grounded
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, location, step);
if (canRotate)
{
transform.LookAt(location);
transform.rotation *= Quaternion.Euler(-90, 90, 0);
canRotate = false;
}
if (transform.position == location)
{
StartCoroutine(ExampleCoroutine());
}
}
void SetRandomPos()
{
location = new Vector3(Random.Range(transform.position.x - 10f, transform.position.x + 10f), transform.position.y, Random.Range(transform.position.z - 10f, transform.position.z + 10f));
canRotate = true;
}
IEnumerator ExampleCoroutine()
{
//yield on a new YieldInstruction that waits for 5 seconds.
yield return new WaitForSeconds(2);
//After we have waited 5 seconds print the time again.
if (transform.position == location)
{
SetRandomPos();
}
}
}
I am not exactly clear what you intend to do, but saw a few issues with the code and tried to fix them. The snippet is untested, I just added what I believe to be the correction that should fix your issues.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
private Vector3 location;
private Coroutine changeMovement = null;
private float speed;
private Vector3 playerVelocity = Vector3.zero;
CharacterController controller;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
speed = 5;
SetRandomPos();
}
void Update()
{
bool groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = (transform.position - location).normalized;
controller.Move(move * Time.deltaTime * speed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
// check if we are close to our goal, then change the goal
if (changeMovement == null && Vector3.Distance(transform.position,location) < 0.1f)
{
changeMovement = StartCoroutine(ExampleCoroutine());
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
void SetRandomPos()
{
location = new Vector3(Random.Range(transform.position.x - 10f, transform.position.x + 10f), transform.position.y, Random.Range(transform.position.z - 10f, transform.position.z + 10f));
}
IEnumerator ExampleCoroutine()
{
//yield on a new YieldInstruction that waits for 5 seconds.
yield return new WaitForSeconds(2);
SetRandomPos();
changeMovement = null;
}
Edit: I changed the code and used the CharacterController.Move as a base.

How to use transform.localEularAngles in Unity

Hello I am new at Unity and I am trying to make a simple 3D first person shooter. To do this I am trying to make a camera follow the mouse cursor. To do this I want to use set the localEularAngles according to the rotation generated by the mouse.
The following is the code of my CameraController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float maximumY = 360f;
public float minimumY = -360f;
public float maximumX = 60f;
public float minimumX = -60f;
public float sensitivityX = 15f;
public float sensitivityY = 15f;
public Camera cam;
float rotationY;
float rotationX;
float offset;
// Start is called before the first frame update
void Start()
{
offset = cam.transform.position.y - transform.position.y;
}
// Update is called once per frame
void Update() {
rotationY += Input.GetAxis ("Mouse X") * sensitivityY;
rotationX += Input.GetAxis ("Mouse Y") * sensitivityX;
rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
transform.localEularAngles = new Vector3(0, rotationY, 0);
cam.transform.localEularAngles = new Vector3(-rotationX, rotationY, 0);
cam.transform.position = new Vector3(cam.transform.position.x,
cam.transform.position.y + offset, cam.transform.position.z);
}
}
The idea behind this is taken from the tutorial at this link: https://www.mvcode.com/lessons/first-person-camera-controls-jamie
However, when trying to compile I get the error: Assets\CameraController.cs(36,31): error CS1061: 'Transform' does not contain a definition for 'localEularAngles' and no accessible extension method 'localEularAngles' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)
The error occurs at both of the places that i use transform.localEularAngles. In fact, I have found no way to write transform.localEularAngles in a way that does compile, which has led me to believe that it might be deprecated, but it seems more likely that I am missing something fundamental.
Just a simple spelling mistake, should be localEulerAngles.
transform.localEulerAngles = new Vector3(0, rotationY, 0);
cam.transform.localEulerAngles = new Vector3(-rotationX, rotationY, 0);
cam.transform.position = new Vector3(cam.transform.position.x,
cam.transform.position.y + offset, cam.transform.position.z);

Unity error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

I just started programing and I'm following a tutorial on how to make a game in unity
So i did everything just like the video said but I'm still getting this error
Can please somebody explain what i did wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float playerSpeed = 500;
public float directionalSpeed = 20;
void Start()
{
// Start is called before the first frame update
}
// Update is called once per frame
void Update() {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER
.transform.position.y, gameObject.transform.position.z), directionalSpeed * Time.deltaTime);
#endif
float moveHorizontal = Input.GetAxis("Horizontal");
transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + moveHorizontal, -2.5f, 2.5f), gameObject
GetComponent<Rigidbody>().velocity = Vector3.forward * playerSpeed * Time.deltaTime;
//Mobile Controls
Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10f));
if (Input.touchCount > 0)
{
transform.position + new Vector3(touch.x, transform.position.y, transform.position.z);
}
}
}
In fact the code that you will get is:
void Update() {
.transform.position.y, ...), directionalSpeed * ...);
}
And this is not valid. That's why.

Unity2D: How to get object to move forward

I am trying to run a simple script to get an object to move forward within unity.
My code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToHold : MonoBehaviour {
private float traveledDistance;
public int moveSpeed = 10;
private bool isMoving;
public GameObject Aircraft;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (isMoving == true)
{
//Aircraft.transform.position += transform.forward * Time.deltaTime * moveSpeed;
Aircraft.transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
public void move ()
{
isMoving = true;
Debug.Log(isMoving);
}
}
As far as I can see, the transform.position should work.
Any ideas?
Try changing :
Aircraft.transform.position += transform.forward * moveSpeed * Time.deltaTime;
to :
Aircraft.transform.position += transform.right * moveSpeed * Time.deltaTime;
Sometimes with unity2D the forward axis is the Z so you're pushing it inside the Z axis which you won't see. Right will move it on the x axis.
I think you need to apply your position to the RigidBody object rather than the aircraft. If I'm guessing right, that should be your aircraft's parent. Try:
Aircraft.parent.transform.position += transform.forward * moveSpeed * Time.deltaTime;

2D Collision detection not working

I've been trying to get collision to work but so far no good.
Screenshot Unity
This is my moveBall.cs which I put on my Ball Object.
using UnityEngine;
using System.Collections;
public class moveBall : MonoBehaviour {
float balSnelheid = 1;
void Update () {
transform.Translate (0, balSnelheid * Time.deltaTime, 0);
}
void OnCollisionTrigger2D (Collision2D coll) {
if (coll.gameObject.name == "Brick") {
Destroy(coll.gameObject);
}
}
}
And this is my movePlayer.cs which I put on my Player Object.
using UnityEngine;
using System.Collections;
public class movePlayer : MonoBehaviour {
public float snelheid;
// Use this for initialization
void Start () {
Screen.showCursor = false;
}
// Update is called once per frame
void Update () {
if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime < 2.9) {
if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime > -2.9) {
transform.Translate(Input.GetAxis("hor") * snelheid * Time.deltaTime, 0, 0);
}
}
if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime > 3) {
transform.position = new Vector3(2.9f, -4.7f, 0);
} else if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime < -3) {
transform.position = new Vector3(-2.9f, -4.7f, 0);
}
}
}
If anyone could give me a tip/solution it would help me out a lot!
One issue I see right off the bat is that you are using the RigidBody component rather than the RigidBody2D component. You need to be careful which components you use.
Also, OnTriggerEnter2D() or OnCollisionEnter2D() is what you are looking for, not OnCollisionTrigger2D.
If using the the 3d components was by choice, please look at OnCollisionEnter() or OnTriggerEnter()

Categories