I am trying to create an interaction system in Unity using Natty Creations tutorial: https://www.youtube.com/watch?v=gPPGnpV1Y1c&t=866s
Although I am having a bit of trouble as the script cant find the Action that I am trying to reference.
I am still a beginner so sorry if this is a stupid question with a simple fix.
Here is the script that I am stuck with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInteract : MonoBehaviour
{
private Camera cam;
[SerializeField]
private float distance = 3f;
[SerializeField]
private LayerMask mask;
private PlayerUI playerUI;
private InputManager inputManager;
// Start is called before the first frame update
void Start()
{
cam = GetComponent<PlayerLook>().cam;
playerUI = GetComponent<PlayerUI>();
inputManager = GetComponent<InputManager>();
}
// Update is called once per frame
void Update()
{
playerUI.UpdateText(string.Empty);
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
Debug.DrawRay(ray.origin, ray.direction * distance);
RaycastHit hitInfo; // var to store collision info
if (Physics.Raycast(ray, out hitInfo, distance, mask))
{
if (hitInfo.collider.GetComponent<Interactible>() != null)
{
Interactible interactible = hitInfo.collider.GetComponent<Interactible>();
playerUI.UpdateText(interactible.promptMessage);
if (inputManager.OnFoot.Interact.triggered)
{
interactible.BaseInteract();
}
}
}
}
And my Input Actions: Unity Input Actions
Any help would be greatly appreciated!
Thanks!
I had the SAMMMMEEE problem but I know the fix know (Thanks to a friend) all you have to do is to go into your InputManager and turn onFoot to public. In your Player Interact in the very last if function chane the Uppercase Onfoot to Lowercase onFoot. That's what works for me!
When you are in Input Actions you have to press Save Asset at the top of the window.
I don't know the problem but it worked for me when I did change "OnFoot" to "onFoot"(as Naeto3452 said) and if change "private" to "public" in InputManager.cs "private PlayerInput.OnFootActions onFoot;" it might help
Related
I was attempting to make a top down 2D shooter using Unity. My code contains no errors that I could see, RigidBody2D and PlayerMovement (code for the player to move) have been added to the sprite, and RigidBody2D has been added to the PlayerMovement. My move speed is set to 5. Please let me know what I can do to fix this issue!
Code:
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public Rigidbody2D rb;
Vector2 movement;
void Update()
{
MovementInput();
}
private void FixedUpdate()
{
rb.velocity = movement * moveSpeed;
}
void MovementInput()
{
float mx = Input.GetAxisRaw("Horizontal");
float my = Input.GetAxisRaw("Vertical");
movement = new Vector2(mx, my).normalized;
}
}
Use your character sprite as the mouse cursor instead to write a code to make the player follow the mouse pos
If you really want to move your player via script this video will help you https://www.youtube.com/watch?v=0Qy3l3VuF_o
Have a good day :)
Check if your rigidbody2D's Body type is set to static, If it is then set it to kinematic or dynamic.
Also where did you import the script to?
Your code is not the problem, I tested it myself! Although you could move MovementInput(); to the FixedUpdate. Its not required though.
Edit: Image of the inspector as i have it if it helps
I am currently working on the Flooded Grounds Unity Asset, I'm implementing an Alien NPC, with a script that tells it to follow the player.
The problem that I'm encountering is that the Alien turns his back to the player and I can't figure out why. I also tried rotating it by 180° on the Y axis (both in the transform and inside of the script using transform.rotate.y - when using the transform doesn't make any difference while using it in the update function just makes it constantly snap back and forth).
Here's a video of the issue:
https://drive.google.com/file/d/1tC9zJWKXXDuNZNZJbl2htBUgTPSTB4IG/view?usp=sharing
And here's the script that I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyFollow : MonoBehaviour
{
public NavMeshAgent enemy;
public Transform Player;
private Rigidbody alienRb;
private Animator alienAnim;
// Start is called before the first frame update
void Start()
{
alienRb = GetComponent<Rigidbody>();
alienAnim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
enemy.SetDestination(Player.position);
if(enemy.velocity.magnitude > 0)
{
alienAnim.SetTrigger("Walk");
}
}
}
Any help would be very appreciated!
Hello Mazza ^^ First of all, I have to say that your alien monster is really cute :D Anyway, did you try transform.LookAt(Player.transform.position)? Also, transform.rotation is might not be affective, maybe you can try alien.transform.Rotate(0.0f, 90.0f, 0.0f, Space.Self); or transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 180f, transform.rotation.z)
so this issue is fixed, the problem was that the U in Update wasnt capitalised
me and a few friends started making a FPS a few weeks ago and I'm now trying to get the AI to just walk towards the player but the enemy just stands still like a random object.
I have tried to rewrite the code, redo the NavMesh, redo the player and enemy components (with help from my brother who is an indie dev), I followed unity's documentation, even tried to do the AI in a different project but it just doesn't work.
I don't get any errors, so I don't know what I'm doing wrong, been at this AI for a day or two now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerNavMesh : MonoBehaviour{
private NavMeshAgent agent;
private GameObject target;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
private void update() {
target = GameObject.FindGameObjectWithTag("Player");
if(target != null)
{
agent.destination = target.transform.position;
}
else
{
Debug.Log("No target found");
}
}
}
Update() should be capitalized. Also, why are you setting target every frame?
I have a problem that main camera position doesn't change after CharacterController.SimpleMove() called. The task is to create scene where camera moves.
I have Main Camera game object with Character Controller and Script attached.
The issue is that nothing in vrCamera position changed after SimpleMove() called.
My question is what is wrong in this code. I suggest something wrong with binding between MainCamera object and CharacterController component, but I have spend a lot of time investigating and nothing working found.
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class VRLookWalk : MonoBehaviour {
public Transform vrCamera;
public float toggleAngle = 30.0f;
public float speed = 3.0f;
public bool moveForwad;
private CharacterController cc;
// Use this for initialization
void Start () {
cc = vrCamera.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
if (vrCamera.eulerAngles.x >= toggleAngle && vrCamera.eulerAngles.x < 90.0f)
{
Vector3 forward = vrCamera.TransformDirection(Vector3.forward);
cc.SimpleMove(forward * speed);
}
}
}
You can't move the VR Camera, it's the SDK that determine the mainCamera position.
In order to move your camera you can just make a new GameObject as a parent of your mainCamera then move the parent GameObject
Try this. Your TransformDirection probably returns wrong vector.
Vector3 forward = vrCamera.transform.forward;
cc.SimpleMove(forward * speed);
Hi guys,
I am working in Unity (C#) and I have a problem. I have a cube called littleOne that is falling down from start position and another cube that destroys the first cube OnTriggerEnter. When the first cube collides with "destroyer" cube, I want to bring it back to the start position. All game objects are attached correctly to the script. This is my solution:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject littleOne;
public Vector3 startPoint;
void Start()
{
startPoint = littleOne.transform.position;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "littleOne") {
other.gameObject.transform.position = startPoint; //*not working
//littleOne.transform.position = startPoint; //*not Working
}
}
}
Lines that has symbol * does nothing, well with these lines I have a problem. I was searching for solution and I tried this way, but nothing happens.
EDIT: PROBLEM SOLVED: I have solved the problem. This code I have moved to a script with falling operation. It was issue in multiple related script to a object. Thank you for recommendations!
Other object and littleOne are referring to the same object which you are destroying:
Dont destroy it:
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "littleOne")
{
littleOne.transform.position = startPoint;
}
}