I am trying to make my first game and have been struggling to get my animations to work.
I have been following along with a YouTube course and when it came to coding the animations on Unity i cannot seem to get it to work.
Please can someone who understands coding and Unity have a look at the following screenshots and help me out.
This is my code for my game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
private Vector3 input;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Verical"));
Rigidbody.AddForce(input * moveSpeed);
}
}
I cannot get the white dot next to my Player Control script to light up and it will not work.
Any help is appreciated, thanks.
If you are able to change the script, then this is how I would do it:
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float speed = 10f;
float x = Input.GetAxis(“Horizontal”) * speed * Time.deltaTime;
float z = Input.GetAxis(“Vertical”) * speed * Time.deltaTime;
rb.AddForce(x, 0f, z);
}
And freeze rotation along the x and a axis to not fall over. You can do this in the rigidbody component.
Related
So i made an ball(player) which moves forward on it's own with script. I want to make that ball act like a normal ball. when it riches the edge of platform it won't fall off. Basicaly it stops on the edge. Here's my image:
Here's my controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwerveInputSystem : MonoBehaviour
{
private float _lastFrameFingerPositionX;
private float _moveFactorX;
public float MoveFactorX => _moveFactorX;
void Start(){
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
_lastFrameFingerPositionX = Input.mousePosition.x;
}
else if (Input.GetMouseButton(0))
{
_moveFactorX = Input.mousePosition.x - _lastFrameFingerPositionX;
_lastFrameFingerPositionX = Input.mousePosition.x;
}
else if (Input.GetMouseButtonUp(0))
{
_moveFactorX = 0f;
}
}
}
This is Second script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour{
private SwerveInputSystem _swerveInputSystem;
[SerializeField] private float swerveSpeed = 5f;
[SerializeField] private float maxSwerveAmount = 1f;
[SerializeField] private float verticalSpeed;
void Start(){
_swerveInputSystem = GetComponent<SwerveInputSystem>();
}
void Update(){
float swerveAmount = Time.deltaTime * swerveSpeed * _swerveInputSystem.MoveFactorX;
swerveAmount = Mathf.Clamp(swerveAmount, -maxSwerveAmount, maxSwerveAmount);
transform.Translate(swerveAmount, 0, 0);
float verticalDelta = verticalSpeed * Time.deltaTime;
transform.Translate(swerveAmount, verticalDelta, 0.1f);
}
}
EDITED: Adjusted the answer since we now have some source code.
You are positioning the player directly (using its transform) which will mess up the physics. The purpose of a rigidbody is to let Unity calculate forces, gravity, and so on for you. When you are using physics, and you want to move an object you have three main options:
Teleporting the object to a new position, ignoring colliders and forces like gravity. In this case use the rigidbody's position property.
_ourRigidbody.position = new Vector3(x, y, z);
Moving the object to the new position, similar to teleporting but the movement can be interrupted by other colliders. So, if there is a wall between the object and the new position, the movement will be halted at the wall. Use MovePosition().
_ourRigidbody.MovePosition(new Vector3(x, y, z));
Adding some force to the object and letting the physics engine calculate how the object is moved. There are several options like AddForce() and AddExplostionForce(), etc... see the Rigidbody component for more information.
_ourRigidbody.AddRelativeForce(new Vector3(x, y, z));
In your case you can simply remove the transsform.Translate() calls and instead add some force like this:
//transform.Translate(swerveAmount, 0, 0);
//transform.Translate(swerveAmount, verticalDelta, 0.1f);
Vector3 force = new Vector3(swerveAmount, verticalDelta, 0);
_ourRigidbody.AddForce(force);
We can get the _ourRigidbody variable in the Awake() or Start() method as normal. As you can see I like the Assert checks just to be safe, one day someone will remove the rigidbody by mistake, and then it is good to know about it...
private SwerveInputSystem _swerveInputSystem;
private Rigidbody _ourRigidbody;
void Start()
{
_swerveInputSystem = GetComponent<SwerveInputSystem>();
Assert.IsNotNull(_swerveInputSystem);
_ourRigidbody = GetComponent<Rigidbody>();
Assert.IsNotNull(_ourRigidbody);
}
One likely reason your Rigidbody is not being affected by gravity is due to having the field isKinematic checked to on. From the Rigidbody docs, when toggling on isKinematic,
Forces, collisions or joints will not affect the rigidbody anymore.
The rigidbody will be under full control of animation or script
control by changing transform.position
As gravity is a force and no forces act on the object when this setting is checked, your object will not fall when it is no longer on the platform. A simple solution is to uncheck this box.
First of all. I am pretty much a blank beginner and was trying to make a little game in unity.
It's a stick that gets addforced up and rotated at the same time.
Now the problem is that when I add the transform.up force, it is bound to the objects z rotate and not the global
Is there any way around that?
using UnityEngine;
public class LaunchCAR : MonoBehaviour
{
public Rigidbody2D rb2D;
public float thrust = 10.0f;
public float torque = 1f;
private void Start()
{
transform.position = new Vector3(0.0f, -2.0f, 0.0f);
}
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
rb2D.AddRelativeForce(-(transform.up) * thrust, ForceMode2D.Impulse);
// float turn = Input.GetAxis("Horizontal");
rb2D.AddTorque(torque, ForceMode2D.Impulse);
}
}
}
AddRelativeForce expects object-space (local) coordinates, but you're passing transform.up which is in world-space coordinates.
Use either Vector3.up or AddForce instead.
Hey guys I want to make a multiplayer game with unity. But I cannot sync players.
I use Photon Transform View and Photon View. I have attached Photon Transform View to Photon View but still it doesnt work.
This is my player movement code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using Photon.Realtime;
using UnityEngine;
public class Movement : Photon.MonoBehaviour
{
joystick griliVAl;
Animator animasyon;
int Idle = Animator.StringToHash("Idle");
// Use this for initialization
void Start () {
animasyon = GetComponent<Animator>();
griliVAl = GameObject.FindGameObjectWithTag("Joystick").GetComponent<joystick>();
}
public void OnButton()
{
animasyon.Play("attack_01");
}
// Update is called once per frame
void Update () {
float x = griliVAl.griliv.x;
float y = griliVAl.griliv.y;
animasyon.SetFloat("Valx", x);
animasyon.SetFloat("Valy", y);
Quaternion targetRotation = Quaternion.LookRotation(griliVAl.griliv*5);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime);
transform.position += griliVAl.griliv * 5 * Time.deltaTime;
}
}
It will be mobile game. So that these griliVal value is joysticks circle.
But can someone please help me to solve this issue?
If by whatever means it doesn't work, try using OnPhotonSerializeView().
Here is what you can put
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
stream.SendNext(transform.position);
}
else if(stream.isReading)
{
transform.position = (Vector3)stream.ReceiveNext();
}
}
It is really similar to using Photon transform View, but you are manually synchronizing the player's position.
Check if PhotonView is attached to the same gameobject the script is in for OnPhotonSerializeView to work.
Don't forget to add IPunObservable in your code
public class Movement : Photon.MonoBehaviour, IPunObservable
Hi everybody, I'm trying to rotate a child game object relative to another child game object. For example, as shown in this heirarchy below, I'm trying to rotate PA_DroneBladeLeft (and PA_DroneBladeRight) with respect to their immediate parents PA_DroneWingLeft (and PA_DroneWingRight) respectively. I want these blades to spin in place. Instead I'm getting them to spin globally in the y-direction relative to the main parent (Air Drone). I would think that the line in Update method that I commented out should have worked, and it did but it still rotated relative to Air Drone and not in place. The second line, RotateAround method I've tried to create an empty game object called Left Pivot and put it approximately in the center of the left wing and have the left blade rotate around that, but it was no use.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeftBladeRotation : MonoBehaviour
{
GameObject leftBlade;
private float speed;
private float angle;
GameObject leftPivot;
// Start is called before the first frame update
void Start()
{
speed = 10f;
leftBlade = transform.Find("PA_DroneBladeLeft").gameObject;
angle = 0f;
leftPivot = GameObject.FindGameObjectWithTag("Air Drone").transform.Find("PA_Drone").transform.Find("PA_DroneWingLeft").transform.Find("Left Pivot").gameObject;
}
// Update is called once per frame
void Update()
{
angle += speed * Time.deltaTime;
Quaternion rotation = Quaternion.Euler(new Vector3(0f, angle, 0f));
//leftBlade.transform.localRotation = rotation;
leftBlade.transform.RotateAround(leftPivot.transform.localPosition, Vector3.up, angle);
}
}
This is your script with some little changes. It actually works when I attached it on the blade gameObject.
using UnityEngine;
public class BladeRotate : MonoBehaviour
{
private float _speed;
private float _angle;
private void Start()
{
_speed = 400f;
_angle = 0f;
}
private void Update()
{
_angle += _speed * Time.deltaTime;
var rotation = Quaternion.Euler(new Vector3(0f, _angle, 0f));
transform.localRotation = rotation;
}
}
The second line does not work because you rotate the blade around global y-axis by Vector3.up
Here is an alternative. Attach the following script on your blades.
using UnityEngine;
public class BladeRotate : MonoBehaviour
{
[SerializeField] private int speed = 400;
private Transform _transform;
private void Start()
{
_transform = transform;
}
private void Update()
{
transform.RotateAround(_transform.position, _transform.up, speed * Time.deltaTime);
}
}
I've been scouring the internet for the past couple of hours trying to find a working solution to this. I've tried everything I could possibly think of: different types of functions, different types of updates, different smoothing times. Below is a video of how my game currently plays. I'm making a small platformer just for practice, and I want to get this camera problem out of the way! Click here for video
Here is my current code, but again, I've tried numerous other combinations, too. Thanks for all the help.
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public Transform target;
public Vector3 offset;
public float smoothTime = 0.3f;
private Vector3 velocity;
private void LateUpdate()
{
transform.position = Vector3.SmoothDamp(transform.position, target.position + offset, ref velocity, smoothTime);
}
}
EDIT:
I've tried a bunch of other suggestions, but nothing is still working. If this helps, I'm running Unity 2018.2.5f1 Personal 64 bit. I'm using a Razer Blade 15 2018.
You can try the code below. This piece of code works for me in my 3D game.
public float translationFactor = 20;
void LateUpdate(){
if(transform.position != target.position) {
transform.position += (target.position - transform.position) / translationFactor;
}
}
This is a direct quote about why you should use LateUpdate() when you are working with cameras, from Unity3D's LateUpdate() documentation.
LateUpdate is called after all Update functions have been called. This
is useful to order script execution. For example a follow camera
should always be implemented in LateUpdate because it tracks objects
that might have moved inside Update.
Also I've noticed that you use Vector3 instead of Vector2 in a 2D game. I'm not experienced in 2D as much as I'm in 3D so I don't know if it will make any difference to replace Vector3s with Vector2s.
I haven't tested this, but it should work if you simply change the function name from LateUpdate to Update, if I understand your problem correctly.
I am thinking your problem on your skeleton script. Your camera follows skeleton and you are thinking problem on camera
Try to move skeleton with AddForce instead of transform.position
Example
void Update () {
if (Input.GetMouseButtonDown (0)) {
GetComponent<AudioSource> ().PlayOneShot (voices[2]);
birdSpeed.velocity = Vector2.zero;
birdSpeed.AddForce (new Vector2 (0, birdUp));
}
public void Buttons(int i){
if (i == 0) {
birds = 0.2f;
GetComponent<Rigidbody2D> ().gravityScale = 4;
birdUp = 400;
StartPanel.SetActive (false);
GamePanel.SetActive (true);
GameOverPanel.SetActive (false);
}
In this code when i click to mouse bird up and you can adapt your code to this
i know im late but for all of you asking this question i looked in to your problem and tested it in a game im working on, and i fixed your issue by changing void LateUpdate() to void FixedUpdate() here is the code! Hope i helped..
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public Transform target;
public Vector3 offset;
public float smoothTime = 0.3f;
private Vector3 velocity;
private void LateUpdate()
{
transform.position = Vector3.SmoothDamp(transform.position, target.position + offset, ref velocity, smoothTime);
}
}