I'm trying to build my first game in Unity 2D.
OS: Windows 10
I reached the moment where I'm supposed to program the controls for a character.
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W))
{
GetComponend<RigidBody2D> ().velocity = new Vector2 (0, jumpHeight);
}
}
}
Trying to create a build ends with this error text:
> Build failed. MSBuild process could not be started
Please have a look at the screen shot below:
Screenshot
How can I solve this problem?
Related
im new to unity game development. i have to develop simple 2D object movement in square path during mouse button clicked. just simple square/circle 2D spirite move in square path during mouse clicked
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;
public class mousetomove : MonoBehaviour
{
public float speed = 5.0f;
private Transform target1;
private Transform target2;
private Transform hero ;
// Start is called before the first frame update
void Start()
{
hero = GameObject.FindGameObjectWithTag("Hero").GetComponent<Transform>();
target1 = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
if (hero == target1)
{
transform.position = Vector2.MoveTowards(transform.position, target2.position ,
*Time.deltaTime);
}
}
}
}
Here you are trying to check if hero is equal to target, this will not happen I think, you have to compare positions but even doing so positions will probably not be the same but just very close. You are using MoveTowards, thats good but you have to be changing the target, like maybe have 4 targets in an array I guess
I'm currently trying to make a camera go to an object when right click is held however it's not working how do I fix this? When the button right click is pressed it prints hi and hello however the camera does not move.
Edit: The line "transform.localPosition = MagicWandReg1Pos;" and " transform.localPosition = CameraReg1Pos;" is what i think is the problem but i just don't know how to fix it.
Edit 2: the problem is the addon cinemachine (i think ) I'm currently working on fixing it).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camscript : MonoBehaviour
{
public Vector3 MagicWandReg1Pos;
public Vector3 CameraReg1Pos;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
MagicWandReg1Pos = new Vector3(0.9525713f,2,2400557f);
CameraReg1Pos = new Vector3(-5.601483f,3,150208);
transform.localPosition = CameraReg1Pos;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
transform.localPosition = MagicWandReg1Pos;
print("Hello");
}
if (Input.GetKeyUp(KeyCode.Mouse1))
{
transform.localPosition = CameraReg1Pos;
print("Hi");
}
}
}
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)
This is a reduced version of the player controller I am using now, which still produces the error explained below:
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private float m_RunSpeed;
private CharacterController m_CharacterController;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
Vector3 move = Vector3.right * m_RunSpeed;
m_CharacterController.Move(move * Time.fixedDeltaTime);
}
}
}
I am using the standard assets script and I am trying to have a script teleport the player to different places. When I try and move the player it goes to that position for a frame, then goes right back to its position.
player.transform = new Vector3(1,2,3);
// works as expected, but then next frame, player's
// position is back to where it was before
This problem occurs because Move may not be able to read the up-to-date values for transform.position.
This problem has been reported on the official Unity Issue Tracker here, and a solution was posted there as well:
The problem here is that auto sync transforms is disabled in the physics settings, so characterController.Move() won't necessarily be aware of the new pose as set by the transform unless a FixedUpdate or Physics.Simulate() called happened in-between transform.position and CC.Move().
To fix that, either enable auto sync transforms in the physics settings, or sync manually via Physics.SyncTransforms right before calling Move()
So, you could fix your problem by editing FixedUpdate so that it calls Physics.SyncTransforms before Move, like this:
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private float m_RunSpeed;
private CharacterController m_CharacterController;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
Vector3 move = Vector3.right * m_RunSpeed;
Physics.SyncTransforms();
m_CharacterController.Move(move * Time.fixedDeltaTime);
}
}
}
This is my simple code - I'm using Visual Studio 2017, and assume the issue is with the libraries that contain the functions Input."" are not being found.
There is no auto complete, when I start to type Input."" and there are no colours indicating it's a prewritten function. Could anyone help?
I've been stuck trying to figure out how to make this work and I'm completely stalemated (btw even when I type 0.5f it does not colour up like it does when I watch other people code this).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horiztonal") > 0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horiztonal") * moveSpeed * Time.deltaTime,0f,0f));
}
}
}